def pingTest(self):					#Ping Test to verify IP address configuration

		topodata = getData('Topology.json')
		Routers = topodata['Device_details'].keys()
		Routers.sort()
		for Router in Routers:

			if Router == "Router1":
				RouterInst = basicConfiguration.switchToRouter(self.device,Router)
				for i in range(1,self.NumberOfDevices):
					NDUT = 'Router' + str(i+1)
					
					Link = "Link_Router1_"+ NDUT
					PeerInterface = topodata["Link_details"][Link][NDUT]
					IPAddress = topodata["Device_details"][NDUT][PeerInterface]
					basicConfiguration.ping(RouterInst,IPAddress)
			
			else:
				RouterInst = basicConfiguration.switchToRouter(self.device,Router)
				NDUT = 'Router1'
				Link = "Link_Router1_" + Router
				PeerInterface = topodata["Link_details"][Link][NDUT]
				IPAddress = topodata["Device_details"][NDUT][PeerInterface]
				basicConfiguration.ping(RouterInst,IPAddress)

		logToFile.info('Ping Test successful on all the devices') 
		return
def setInterfaceUp(device,RouterPid,intfList):							#Brings the interfaces up

	#flushBuffer(1,device)

	j = 0
	i = 1
	maxiter = len(intfList)
	while j != maxiter:
	
		if j % 2 == 0:
			
			flushBuffer(1,device)	
			cmd = """sudo ip link set %s netns %s
			sudo ip netns exec %s ip link set %s up""" %(intfList[j],RouterPid[0],RouterPid[0],intfList[j])
			device.sendline(cmd)
			device.expect(['w+@.*/#',pexpect.EOF,pexpect.TIMEOUT],timeout=3)
			logToFile.info('		Interface %s of Router1 UP',intfList[j])
			logger.info(device.before,also_console=True)
			j = j+1

		else:
			flushBuffer(1,device)
			cmd = """sudo ip link set %s netns %s
			sudo ip netns exec %s ip link set %s up""" %(intfList[j],RouterPid[i],RouterPid[i],intfList[j])
			device.sendline(cmd)
			device.expect(['w+@.*/#',pexpect.EOF,pexpect.TIMEOUT],timeout=3)
			logToFile.info('		Interface %s of Router%d UP',intfList[j],i+1) 
			logger.info(device.before,also_console=True)
			j = j+1
			i = i+1	

	return 
	def getIPaddressList(self,network_list,intflist):	#Gets the list of IP addresses from the pool of available IPs in a given network,that should be alloted to devices   			
		logToFile.info("Getting the IP address List")									
		IPaddressList = []
		for i in network_list:
			IPaddressList = basicConfiguration.getIPaddList(unicode(i),IPaddressList)
		logToFile.info('	  IP address list that can be allocated to %s interfaces is %s respectively',intflist,IPaddressList)
		return IPaddressList
	def buildNetworkTopology(self):				#Builds Network Topology i:e instantiate devices(docker containers),load containers with latest flexswitch image,addlinks and set interfaces UP 

		print "In build network"
		TopologyType = self.data['TopologyType']
		image = self.data["flexswitchImage"]
		logToFile.info('Building Network Topology')		
		TopologyDetails = buildNetwork(TopologyType,image)
		logToFile.info('	Network Topology of type %s is built with %d devices having %s as interfaces of devices and populated with %s networks' ,TopologyType,self.NumberOfDevices,TopologyDetails["Interfaceslist"],TopologyDetails["Nwlist"])
		return TopologyDetails
    def enableBGPGlobal(self, Device):  #Enables BGP global

        logToFile.info("	  Enabling BGP Global")
        data = getData('ProtocolSpecific.json')
        RouterID = data['BGP_Parameters'][Device]['RouterID']
        AS = data['BGP_Parameters'][Device]['ASNum']
        RouterInst = BGPConfiguration.switchToRouter(self.device, Device)
        RouterInst = BGPConfiguration.BGPglobal(RouterInst, AS, RouterID)
        return RouterInst
    def setPeersDetails(
        self
    ):  #Determines the directly connected neighbors(peers) and update the related information about the peer to protocol specific JSON file

        for i in range(0, (self.NumberOfDevices)):
            Router = 'Router%d' % (i + 1)
            BGPdata = BGPConfiguration.setPeerDetails(Router)
        logToFile.info(
            "		ProtocolSpecific.json file is updated with all the BGP parameters for all the devices"
        )
        logToFile.info("		Protocol Specific JSON is as shown below \n   %s",
                       BGPdata)
        return
    def checkBGPNeighbors(self, Router):  #Check if BGP neighbors are learnt

        data = getData('ProtocolSpecific.json')

        if (isinstance(Router, list)):

            for i in Router:
                logToFile.info("Checking if BGP neighbors are set in %s", i)
                RouterInst = switchToRouter(self.device, i)
                RouterInst = BGPConfiguration.checkAllBGPNeighbors(RouterInst)
                RouterInst = BGPConfiguration.checkIPV4Route(RouterInst)
                RouterInst.sendline('exit')
            logToFile.info("Verification of BGP Neighbors done in %s", i)

        elif Router == 'all':

            for i in data['BGP_Parameters'].keys():

                logToFile.info("Checking if BGP neighbors are set in %s", i)
                RouterInst = BGPConfiguration.switchToRouter(self.device, i)
                RouterInst = BGPConfiguration.checkAllBGPNeighbors(RouterInst)
                RouterInst = BGPConfiguration.checkIPV4Route(RouterInst)
                #RouterInst = BGPConfiguration.checkBGPRoute(RouterInst)
                RouterInst.sendline('exit')
            logToFile.info("Verification of BGP neighbors done in %s", i)
        return
def addLinks(device,intfList):									#Adds Links between devices

	flushBuffer(1,device)
	
	i = 0 ; j = 2
	maxiter =  len(intfList)
	while i != maxiter:

		cmd = """sudo ip link add %s type veth peer name %s""" %(intfList[i],intfList[i+1])
		device.sendline(cmd)
		device.expect(['w+@.*/#',pexpect.EOF,pexpect.TIMEOUT],timeout=8)
		logger.info(device.before,also_console=True)
		logToFile.info("		link Router1(%s)-----------------------------%s(Router%d)",intfList[i],intfList[i+1],j)
		i = i + 2
		j = j + 1
	return
def preSetup(image):
	
	data = getData('variable.json')								#Get the json data from file "variable.json"
	NumberOfDevices = data["NumberOfDevices"]
	device = pexpect.spawn("/bin/bash")							#Spawn the process ("/bin/bash")- Terminal
	logToFile.info("	  Pulling the flexswitch base image")
	device.expect(['/$',pexpect.EOF,pexpect.TIMEOUT],timeout=1)
	cmd = "sudo docker pull %s" %(image)
	device.sendline(cmd)									#Pull the flexswitch image from docker hub
	device.expect(['/w+@.*/#',pexpect.EOF,pexpect.TIMEOUT],timeout=2)
	device.expect(['/$',pexpect.EOF,pexpect.TIMEOUT],timeout=2)
	logToFile.info("	  Creating  directory for storing network namespace") 
	device.sendline("mkdir -m 777 -p /var/run/netns")					#Create a directory
	device.expect(['netns',pexpect.EOF,pexpect.TIMEOUT],timeout=2)
	logger.info(device.before,also_console=True)
	return device
def checkInterface(RouterInst,Interface,IPaddress):				#Checks if interfaces are UP

	flushBuffer(1,RouterInst)
	RouterInst.sendcontrol('m')
	RouterInst.sendline("ifconfig")
	RouterInst.expect(['/w+@.*/#',pexpect.EOF,pexpect.TIMEOUT],timeout=1)
	logger.info(RouterInst.before,also_console=True)
	output = RouterInst.before
	ip = "inet addr:%s" %(IPaddress)
	status = re.search(Interface,output)
	con = re.search(ip,output)
	if (status and con):
		return
	else:
		logToFile.info("Interface %s not UP",Interface)
		raise RuntimeError("Some error : Interface is not UP")
	def setTopologyFile(self,intfList,IPaddressList):	#Adds all the topology reated details to a file called Topology.json
		
		logToFile.info("Setting up the topology file")
		Network = self.data["Network"]
		subnet = Network.split("/")
		DATA = {}
		DATA.update({"Device_details" : {}})

		for i in range(1,(self.NumberOfDevices+1)):
			Router = "Router"+str(i)
			DATA["Device_details"].update({Router : {}})
		j = 0 ; i = 2
		maxiter = len(intfList)

		while j != maxiter:
			if j % 2 == 0:
				DATA["Device_details"]["Router1"].update({intfList[j] : IPaddressList[j] + '/'+ subnet[1]})
				j = j + 1 
			else:
				Router = "Router" + str(i)
				DATA["Device_details"][Router].update({intfList[j] : IPaddressList[j] + '/' + subnet[1]})
				j = j + 1
				i = i + 1
		putData(DATA,'Topology.json')

		
		DATA.update({'Link_details' : {}})

		for i in range(1,(self.NumberOfDevices)):
			DATA['Link_details'].update({"Link_Router1_Router" + str(i+1) : {}})
		putData(DATA,'Topology.json')
		links = DATA["Link_details"].keys()
		links.sort()
		m = 0 ; n = 1
		
		for i in links:
			
			DATA['Link_details'][i].update({"Router1" : intfList[m],"Router"+str(n+1) : intfList[m+1]})
			m = m+2
			n = n+1
			
		putData(DATA,'Topology.json')	
		logToFile.info('	  Topology file is updated with below device details \n %s',DATA)
		return 
    def setBGPParameters(
        self
    ):  #Adds prerequisite BGP related parameters of all the devices to a file caleed Protocol Specific JSON file

        logToFile.info(
            "	  Setting up the ProtocolSpecific.json file with BGP related Parameters"
        )
        DATA = {}
        DATA.update({'BGP_Parameters': {}})
        RouterIDList = BGPConfiguration.getRouterIDList()
        ASNumList = BGPConfiguration.getASNumList(self.AS)
        for i in range(0, (self.NumberOfDevices)):
            Router = 'Router%d' % (i + 1)
            DATA['BGP_Parameters'].update(
                {Router: {
                    'RouterID': RouterIDList[i],
                    'ASNum': ASNumList[i]
                }})
        putData(DATA, 'ProtocolSpecific.json')
        return
def ping(RouterInst,IPAddress):							#Performs ping test to verify configuration of IP address on interfaces 

	IP = IPAddress.split('/')
	flushBuffer(1,RouterInst)
	RouterInst.sendcontrol('m')
	RouterInst.expect(['/w+@.*/#',pexpect.EOF,pexpect.TIMEOUT],timeout=3)
	cmd = "ping -c 3 %s" %(IP[0])
	RouterInst.sendline(cmd)
	RouterInst.sendcontrol('m')
	RouterInst.expect(['min/avg/max/mdev',pexpect.EOF,pexpect.TIMEOUT],timeout=3)
	output = RouterInst.before
	logger.info(RouterInst.before,also_console=True)
	
	if re.search("0% packet loss",output):
		logToFile.info("Ping Success: Device Reachable")
	else:
		logToFile.debug("Device Unreachable : Check IP address Configuration")
		raise RuntimeError("Error : Device Unreachable")

	return
	
	
		
	def configureIPaddress(self):				#Configures IP address 
		
		data = getData('Topology.json')
		Devices = data['Device_details']
		Routers = Devices.keys()
		Routers.sort()
		for Router in Routers:
			logToFile.info('Configuring ip address on Device %s',Router)
			RouterInst = basicConfiguration.switchToRouter(self.device,Router)
 			RouterInst = basicConfiguration.preliminaryInstalls(RouterInst)
			#RouterInst = basicConfiguration.boot(RouterInst)
			interfaces = Devices[Router].keys()
			interfaces.sort()
			for intf in interfaces:
				interface = intf
				ipadd = Devices[Router][intf]
				RouterInst = basicConfiguration.configureIP(RouterInst,interface,ipadd)
				logToFile.info('	  IP address for %s interface of device %s is configured',interface,Router)
			RouterInst.sendline('exit')
			logToFile.info('IP address configured on all of the interfaces in %s',Router)
		return 
Example #15
0
def outputCheck(RouterInst, var, output):  #Interprets output

    if var == 'BGPglobal':

        if re.search('\"Result\"\:\"Success\"', output):
            logToFile.info("Global BGP Configuration Done")

        elif re.search('\"Result\"\:\"Error\: Nothing to be updated\.\"',
                       output):
            logToFile.info("Global BGP already configured")

    elif var == 'createBGPV4Neighbor':

        if re.search('\"Result\"\:\"Success\"', output):
            logToFile.info("BGP Neighbor configuration done")

        elif re.search('\"Result\"\:\"Error\: Nothing to be updated\.\"',
                       output):
            logToFile.info("BGP Neighbor already configured")

    elif var == 'checkAllBGPNeighbors':

        if re.search('\"ObjectId\":\s[\d|\w|\-|\"]+', output):
            logToFile.info("Verification-BGP Neighbors----PASS")

        else:
            logToFile.info("BGP Neighbors not configured properly")

    elif var == 'checkIPV4Route':

        if re.search('\"ObjectId\":\s[\d|\w|\-|\"]+', output):
            logToFile.info(
                "Verification-BGP Neighbors : Checking IPV4 route----PASS")

        else:
            logToFile.info("BGP Neighbors not configured properly")

    elif var == 'checkBGPRoute':

        if re.search('\"ObjectId\":\s[\d|\w|\-|\"]+', output):
            logToFile.info(
                "Verification-BGP Neighbors : Checking BGP route table----PASS"
            )

        else:
            logToFile.info("BGP Neighbors not configured properly")
	def bootUpDevices(self):				#Boots all instantiated network devices
		
		logToFile.info("Booting up all the devices")
		basicConfiguration.boot(self.device)
		logToFile.info('All the %d network devices booted',self.NumberOfDevices)
		return
Example #17
0
def buildNetwork(TopologyType,image):

	RouterPid = []
	data = getData('variable.json')
	NumberOfDevices = data["NumberOfDevices"]
	logToFile.info("	  Setting up the pre-requisite environment")
	device = Topology.preSetup(image)
	logToFile.info("	  Setting up the nodes of the network")

	for i in range(0,NumberOfDevices):						
	
		NodeID = Topology.createNodes(i,device)					 
		count = 3		
		while count >= 0:

			if NodeID:

				logToFile.info("		NODE CREATED(Router%d)",i+1)	
				RouterPid.append(NodeID.group(1))
				cmd = """ln -s /proc/%s/ns/net /var/run/netns/%s""" %(RouterPid[i],RouterPid[i])
				device.sendline(cmd)
				device.expect(['/d+',pexpect.EOF,pexpect.TIMEOUT],timeout=3)
				logger.info(device.before,also_console=True)
				break
			else:

				cmd = "sudo docker rm -f Router%d" %(i+1)
				device.sendline(cmd)
				NodeID = Topology.createNodes(i,device)
				count = count - 1 
				if count == -1:
					logToFile.debug("Unable to instantiate container for node creation")
					raise RuntimeError("Some error : Unable to instantiate docker container for Router") 
				else:
					continue


	logToFile.info("	  Adding links between routers")

	if TopologyType == 'Star':

		NumberOfInterfaces = (2 * NumberOfDevices) - 2 
		NumberOfNetworks = NumberOfDevices - 1
		intfList = []
		nwList = [] 
	
		for i in range(1,(NumberOfInterfaces+1)):
			intfList.append('eth' + str(i))

		for i in range(0,(NumberOfNetworks+1)):
			nwStart = data["Network"]
			nw = nwStart.split('/')
			nwOctets = nw[0].split('.')
			nwList.append(nwOctets[0] + '.' + nwOctets[1] + '.' + str(i+1) + '.' + nwOctets[3]+ '/' + nw[1])
		
		Topology.addLinks(device,intfList)  
		

		logToFile.info("	  Setting the interfaces UP")

		Topology.setInterfaceUp(device,RouterPid,intfList)
		TopoDet = {}
		TopoDet.update({'Interfaceslist' : intfList, 'Nwlist': nwList})

	return TopoDet
def outputCheck(RouterInst,Interface,IP_address,config):			#Interprets Output
	
	li = IP_address.split('/')
	count = 3
	RouterInst.expect(['/w+@.*/#',pexpect.EOF,pexpect.TIMEOUT],timeout=3)
	logger.info(RouterInst.before,also_console=True)
	output = RouterInst.before

	while count >= 0:

		if re.search('\"Result\"\:\"Success\"',output):
			checkInterface(RouterInst,Interface,li[0])
			break

		elif re.search('Failed to connect to localhost port 8080',output):

			logger.info("***        ERROR:Failed to connect to localhost port 8080               ***",also_console=True)
			logger.info( '***   Flexswitch not started properly : Please restart the flexswitch   ***',also_console=True)
			restartSwitch(RouterInst)
			RouterInst = executeCmd(RouterInst,config)
			RouterInst.expect(['/w+@.*/#',pexpect.EOF,pexpect.TIMEOUT],timeout=3)
			logger.info(RouterInst.before,also_console=True)
			output = RouterInst.before
			count = count - 1
			if count == -1:
				logToFile.debug("Error: Unable to start the flexswitch")
				raise RuntimeError("Some error : Unable to start the Flexswitch")
			else:
				continue


		elif re.search('System not ready',output):

			logger.info("***         ERROR:System not ready,Daemons still restarting		***",also_console=True)
			time.sleep(20)
			RouterInst = executeCmd(RouterInst,config)
			RouterInst.expect(['/w+@.*/#',pexpect.EOF,pexpect.TIMEOUT],timeout=3)
			logger.info(RouterInst.before,also_console=True)
			output = RouterInst.before
			count = count - 1
			if count == -1:
				logToFile.debug("Error: Unable to start the flexswitch")
				raise RuntimeError("Some error : Unable to start the Flexswitch")
			else:
				continue

		elif re.search("Invalid interface reference provided in ip intf config object", output):
			
			logToFile.debug("Invalid interface reference provided in ip intf config object")
			raise RuntimeError("Error : Interface reference provided, is incorrect(Check interface is up for that particular device)")

		elif re.search("ip address validation failed",output):

			logToFile.debug("ip address validation failed")
			raise RuntimeError("Error : IP address not specified correctly")

		elif re.search('\"Result\"\:\"Error\: Already configured\.',output):
			
			logToFile.info("IP address already configured")
			break

		elif re.search('curl : command not found',output):

			preliminaryInstalls(RouterInst)
			count = count -1
			if count == -1:
				logToFile.debug("Unable to install CURL : Check the internet connection")
				raise RuntimeError("Some error : Unable to install curl")
			else:
				continue
	return
    def createBGPNeighbor(self, Router):  #Creates BGP Neighbor

        data = getData('ProtocolSpecific.json')

        if (isinstance(Router, list)):

            for i in Router:
                logToFile.info("Configuring BGP in %s", i)
                RouterInst = self.enableBGPGlobal(i)
                Peers = data["BGP_Parameters"][i]['PeerDetails']
                logToFile.info("	  Setting up the neighbors")
                for peer in Peers.keys():
                    PeerAS = data["BGP_Parameters"][i]['PeerDetails'][peer][
                        'ASNum']
                    PeerAddress = data["BGP_Parameters"][i]['PeerDetails'][
                        peer]['IP_Address']
                    PeerInterface = data["BGP_Parameters"][i]['PeerDetails'][
                        peer]['Interface']
                    RouterInst = BGPConfiguration.createBGPV4Neighbor(
                        RouterInst, PeerAS, PeerAddress, PeerInterface)
                    RouterInst.sendline('exit')
                logToFile.info("BGP configuration donein %s", i)

        elif Router == 'all':

            Routers = data['BGP_Parameters'].keys()
            Routers.sort()
            for Router in Routers:

                logToFile.info("Configuring BGP in %s", Router)
                self.device.expect(['/w+@.*/#', pexpect.EOF, pexpect.TIMEOUT],
                                   timeout=3)
                RouterInst = self.enableBGPGlobal(str(Router))
                Peers = data["BGP_Parameters"][str(Router)]['PeerDetails']
                logToFile.info("	  Setting up the neighbors")

                for peer in Peers.keys():

                    PeerAS = str(data["BGP_Parameters"][str(Router)]
                                 ['PeerDetails'][str(peer)]['ASNum'])
                    PeerAddress = str(data["BGP_Parameters"][str(Router)]
                                      ['PeerDetails'][str(peer)]['IP_Address'])
                    PeerInterface = str(data["BGP_Parameters"][str(
                        Router)]['PeerDetails'][str(peer)]['Interface'])
                    NeighborAddress = PeerAddress.split('/')
                    RouterInst = BGPConfiguration.createBGPV4Neighbor(
                        RouterInst, PeerAS, NeighborAddress[0], PeerInterface)
                RouterInst.sendline('exit')
                logToFile.info("BGP configuration done in %s", Router)
        return