def update_info_router(name_file, router):
    in_bytes = -1
    out_bytes = -1
    debug = 0
    f = open("VFiles/" + name_file, "r")
    lines = f.readlines()
    if_list = router.get_interfaces()
    ifs_names = []
    for interface in if_list:
        ifs_names.append(interface.get_name())

    timeUp_diff = router.get_timeUp_diff(get_timeUp_from_namefile(name_file))
    timeUp_diff = int(convert_in_second(timeUp_diff))
    for idx, name_if in enumerate(ifs_names):
        for line in lines:
            if name_if in line:

                tmp = line.split(",", 1)[1]
                tmp = tmp.split(",", 1)[1]
                tmp = tmp.split(",", 1)[1]
                in_bytes = int(tmp.split(", ", 1)[0])
                out_bytes = int(tmp.split(", ", 1)[1])
                in_diff, out_diff = router.get_inout_bytes_diff(in_bytes, out_bytes, idx)
                if_speed = float(router.get_single_interface(idx).get_if_speed())

                in_utilization = (in_diff * 8 * 100) / (if_speed * timeUp_diff)
                out_utilization = (out_diff * 8 * 100) / (if_speed * timeUp_diff)
                router.set_inout_utilization_if(idx, in_utilization, out_utilization)

    f.close()
    delete_file(name_file)  ##Since that the file is not anymore useful, has to be deleted
    print name_file, " deleted"
    return router
def get_utilization_polling(routers_list, how_much_often, community_name):
	"""Given in input the routers objects list, the interval period and the community name, this function prints out the utilization of each interface for each router. The method used to retrive such information is through SNMP protocol"""
	n_rel=0;
	in_utilization=0
	out_utilization=0

	while True:
		n_rel=n_rel+1;
		time.sleep(how_much_often);
		for router in routers_list:
			ifs_list=router.get_interfaces();
			print '\n###',n_rel, '(Router ',router.get_hostname(),')'
		
			timeUp=get_timeUp_SNMP(router, community_name)
			timeUp_diff=router.get_timeUp_diff(int(timeUp))

			for interface in ifs_list:
				if_id=int(interface.get_id())
				if_name=interface.get_name()
				if_speed=float(interface.get_if_speed())

				ifInBytes, ifOutBytes = get_in_out_bytes_SNMP(router.get_address(), if_id, community_name)
				delta_ifInBytes=int(ifInBytes)- interface.get_old_in_byte()
				delta_ifOutBytes=int(ifOutBytes)-interface.get_old_out_byte()
				debug=0
				if(debug):
					
					print 'Interface: ',interface.get_name()
					print '[NEW]','IN_Bytes: ', ifInBytes, 'OUT_Bytes: ',ifOutBytes, 'Time_Up: ', timeUp
					print '[OlD]','IN_Bytes: ', interface.get_old_in_byte(), 'OUT_Bytes: ',interface.get_old_out_byte(), 'Time_Up: ', router.get_old_timeUp()
					print 'Delta values: ', delta_ifInBytes, delta_ifOutBytes, timeUp_diff

				interface.set_old_in_byte(ifInBytes)
				interface.set_old_out_byte(ifOutBytes)
				##The utilization takes into account also the 	packets get from SNMP
				if(n_rel>1):
					format(in_utilization,'.3f')
					format(out_utilization,'.3f')
					in_utilization=((delta_ifInBytes)*8*100)/(if_speed*(float(timeUp_diff)/100));
					out_utilization=((delta_ifOutBytes)*8*100)/(if_speed*(float(timeUp_diff)/100));
				
				
				##Some times the shown utilization is bigger than one. This it could be due to buffer o burst packets incoming. In order to avoid this kind of situation, it will be one if it is greater than one. 
				
				#if (in_utilization>100): in_utilization=100
				#if (out_utilization>100): out_utilization=100
				

					print '(#', n_rel, ')', ' The  in utilization ', if_name,'interface (ID:', if_id,') is ', in_utilization, '% (if speed: ',if_speed,' )';
					print '(#', n_rel, ')', ' The  out utilization ', if_name,'interface (ID:', if_id,') is ', out_utilization, '% (if speed: ',if_speed,' )\n';
def get_utilization_polling(routers_list, how_much_often, community_name):
    n_rel = 0
    in_utilization = 0
    out_utilization = 0

    while True:
        n_rel = n_rel + 1
        time.sleep(how_much_often)
        for router in routers_list:
            ifs_list = router.get_interfaces()
            print "\n###", n_rel, "(Router ", router.get_hostname(), ")"

            timeUp = get_timeUp_SNMP(router, community_name)
            timeUp_diff = router.get_timeUp_diff(int(timeUp))

            for interface in ifs_list:
                if_id = int(interface.get_id())
                if_name = interface.get_name()
                if_speed = float(interface.get_if_speed())

                ifInBytes, ifOutBytes = get_in_out_bytes_SNMP(router.get_address(), if_id, community_name)
                delta_ifInBytes = int(ifInBytes) - interface.get_old_in_byte()
                delta_ifOutBytes = int(ifOutBytes) - interface.get_old_out_byte()
                debug = 0
                if debug:

                    print "Interface: ", interface.get_name()
                    print "[NEW]", "IN_Bytes: ", ifInBytes, "OUT_Bytes: ", ifOutBytes, "Time_Up: ", timeUp
                    print "[OlD]", "IN_Bytes: ", interface.get_old_in_byte(), "OUT_Bytes: ", interface.get_old_out_byte(), "Time_Up: ", router.get_old_timeUp()
                    print "Delta values: ", delta_ifInBytes, delta_ifOutBytes, timeUp_diff

                interface.set_old_in_byte(ifInBytes)
                interface.set_old_out_byte(ifOutBytes)
                ##The utilization takes into account also the 	packets get from SNMP
                if n_rel > 1:
                    format(in_utilization, ".3f")
                    format(out_utilization, ".3f")
                    in_utilization = ((delta_ifInBytes) * 8 * 100) / (if_speed * (timeUp_diff / 100))
                    out_utilization = ((delta_ifOutBytes) * 8 * 100) / (if_speed * (timeUp_diff / 100))

                    ##Some times the shown utilization is bigger than one. This it could be due to buffer o burst packets incoming. In order to avoid this kind of situation, it will be one if it is greater than one. It is correct this line of thinking ?

                    # if (in_utilization>100): in_utilization=100
                    # if (out_utilization>100): out_utilization=100

                    print "(#", n_rel, ")", " The  in utilization ", if_name, "interface (ID:", if_id, ") is ", in_utilization, "% (if speed: ", if_speed, " )"
                    print "(#", n_rel, ")", " The  out utilization ", if_name, "interface (ID:", if_id, ") is ", out_utilization, "% (if speed: ", if_speed, " )\n"
def get_utilization_single_router_polling(single_router, community_name):
	"""##Giving in input the router object and the community name, this funcion returns the utilization from a single router through SNMP polling"""
	router=single_router[0]
	n_gets=0
	in_utilization=0
	out_utilization=0
	print 'Router: ',router.get_hostname()

	while(n_gets<2):
		ifs_list=router.get_interfaces()
		timeUp=get_timeUp_SNMP(router, community_name)
		timeUp_diff=router.get_timeUp_diff(int(timeUp))
		
		for interface in ifs_list:##For each interface in the router will be get the number of input and output byes
			if_id=int(interface.get_id())
			if_name=interface.get_name()
			if_speed=int(interface.get_if_speed())
			ifInBytes, ifOutBytes = get_in_out_bytes_SNMP(router.get_address(), if_id, community_name)
			delta_ifInBytes=int(ifInBytes)- interface.get_old_in_byte()
			delta_ifOutBytes=int(ifOutBytes)-interface.get_old_out_byte()
			
			debug=0
			if(debug):
				print 'Interface: ',interface.get_name()
				print '[NEW]','IN_Bytes: ', ifInBytes, 'OUT_Bytes: ',ifOutBytes, 'Time_Up: ', timeUp
				print '[OlD]','IN_Bytes: ', interface.get_old_in_byte(), 'OUT_Bytes: ',interface.get_old_out_byte(), 'Time_Up: ', router.get_old_timeUp()
				print 'Delta values: ', delta_ifInBytes, delta_ifOutBytes, timeUp_diff

			interface.set_old_in_byte(ifInBytes)
			interface.set_old_out_byte(ifOutBytes)
		##The utilization takes into account also the packets get from SNMP
			format(in_utilization,'.3f')
			format(out_utilization,'.3f')
			in_utilization=(float(delta_ifInBytes)*8*100)/(if_speed*float(timeUp_diff)/100);
			out_utilization=(float(delta_ifOutBytes)*8*100)/(if_speed*float(timeUp_diff)/100);
			interface.set_in_out_utilization(in_utilization, out_utilization)
		n_gets=n_gets+1
		print '#####',n_gets
	router.set_interfaces(ifs_list)
	return router
def get_utilization_single_router_polling(single_router, community_name):
    router = single_router[0]
    n_gets = 0
    in_utilization = 0
    out_utilization = 0
    print "Router: ", router.get_hostname()

    while n_gets < 2:
        ifs_list = router.get_interfaces()
        timeUp = get_timeUp_SNMP(router, community_name)
        timeUp_diff = router.get_timeUp_diff(int(timeUp))

        for interface in ifs_list:  ##For each interface in the router will be get the number of input and output byes
            if_id = int(interface.get_id())
            if_name = interface.get_name()
            if_speed = int(interface.get_if_speed())
            ifInBytes, ifOutBytes = get_in_out_bytes_SNMP(router.get_address(), if_id, community_name)
            delta_ifInBytes = int(ifInBytes) - interface.get_old_in_byte()
            delta_ifOutBytes = int(ifOutBytes) - interface.get_old_out_byte()

            debug = 0
            if debug:
                print "Interface: ", interface.get_name()
                print "[NEW]", "IN_Bytes: ", ifInBytes, "OUT_Bytes: ", ifOutBytes, "Time_Up: ", timeUp
                print "[OlD]", "IN_Bytes: ", interface.get_old_in_byte(), "OUT_Bytes: ", interface.get_old_out_byte(), "Time_Up: ", router.get_old_timeUp()
                print "Delta values: ", delta_ifInBytes, delta_ifOutBytes, timeUp_diff

            interface.set_old_in_byte(ifInBytes)
            interface.set_old_out_byte(ifOutBytes)
            ##The utilization takes into account also the packets get from SNMP
            format(in_utilization, ".3f")
            format(out_utilization, ".3f")
            in_utilization = (float(delta_ifInBytes) * 8 * 100) / (if_speed * (timeUp_diff / 100))
            out_utilization = (float(delta_ifOutBytes) * 8 * 100) / (if_speed * (timeUp_diff / 100))
            interface.set_in_out_utilization(in_utilization, out_utilization)
        n_gets = n_gets + 1
        print "#####", n_gets
    router.set_interfaces(ifs_list)
    return router
def update_info_router(name_file, router):
	"""Given the name_file (which is the most recent for a specific router) and a router object, this function prints out the in and out utilization of all interfaces of a router. The router object is passed as parameter"""
	in_bytes=-1;
	out_bytes=-1;
        debug=0
	f=open('VFiles/'+name_file, 'r')
	lines= f.readlines()
	if_list=router.get_interfaces()
	ifs_names=[];
	ifs_indexes=[]
	for interface in if_list:
		ifs_names.append(interface.get_name())
		ifs_indexes.append(interface.get_id())
		
	timeUp_diff=router.get_timeUp_diff(get_timeUp_from_namefile(name_file))
	timeUp_diff=int(convert_in_second(timeUp_diff))	
	
	for idx,name_if in enumerate(ifs_names):
		for line in lines:
			if name_if in line:
				
				tmp=line.split(",",1)[1]	
				tmp=tmp.split(",",1)[1]
				tmp=tmp.split(",",1)[1]
				in_bytes=int(tmp.split(", ",1)[0])
				out_bytes=int(tmp.split(", ",1)[1])
				in_diff, out_diff =router.get_inout_bytes_diff(in_bytes, out_bytes,ifs_indexes[idx])##here
				
                                if_speed=float(router.get_single_interface(ifs_indexes[idx]).get_if_speed())
				
				in_utilization=(in_diff*8*100)/(if_speed*timeUp_diff)
                                out_utilization=(out_diff*8*100)/(if_speed*timeUp_diff)
				router.set_inout_utilization_if(ifs_indexes[idx], in_utilization, out_utilization)
		
        f.close()
	delete_file(name_file) ##Since that the file is not anymore useful, has to be deleted
	if(debug):
		print name_file, ' deleted'
	return router