import ConfigParser, re

class confCCNHost():

    def __init__(self, name, app='', uri_tuples='', cpu=None, cores=None, cache=None):
        self.name = name
        self.app = app
        self.uri_tuples = uri_tuples
        self.cpu = cpu
        self.cores = cores
	self.cache = cache

    def __repr__(self):
        return 'Name: ' + self.name + ' App: ' + self.app + ' URIS: ' + str(self.uri_tuples) + ' CPU:' + str(self.cpu) + ' Cores:' +str(self.cores) + ' Cache: ' + str(self.cache)

class confCCNLink():

    def __init__(self,h1,h2,linkDict=None):
        self.h1 = h1
        self.h2 = h2
        self.linkDict = linkDict

    def __repr__(self):
        return 'h1: ' + self.h1 + ' h2: ' + self.h2 + ' params: ' + str(self.linkDict)

def parse_hosts(conf_arq):
    'Parse hosts section from the conf file.'
    config = ConfigParser.RawConfigParser()
    config.read(conf_arq)

    hosts = []

    items = config.items('nodes')
	
	#makes a first-pass read to nodes section to find empty nodes sections
    for item in items:
	name = item[0]
	rest = item[1].split()
	if len(rest) == 0:
		config.set('nodes', name, '_')
	#updates 'items' list
    items = config.items('nodes')

	#makes a second-pass read to nodes section to properly add hosts
    for item in items:

        name = item[0]

        rest = item[1].split()

        app = rest.pop(0)

        uris = rest
        uri_list=[]
        cpu = None
        cores = None
	cache = None

        for uri in uris:
            if re.match("cpu",uri):
                cpu = float(uri.split('=')[1])
            elif re.match("cores",uri):
                cores = uri.split('=')[1]
	    elif re.match("cache",uri):
		cache = uri.split('=')[1]
	    elif re.match("mem",uri):
		mem = uri.split('=')[1]	   	
            else:
                uri_list.append((uri.split(',')[0],uri.split(',')[1]))

        hosts.append(confCCNHost(name , app, uri_list,cpu,cores,cache))

    return hosts

def parse_routers(conf_arq):
    'Parse routers section from the conf file.'
    config = ConfigParser.RawConfigParser()
    config.read(conf_arq)

    routers = []

    items = config.items('routers')

    for item in items:
        name = item[0]

        rest = item[1].split()

        uris = rest
        uri_list=[]
        cpu = None
        cores = None

        for uri in uris:
            if re.match("cpu",uri):
                cpu = float(uri.split('=')[1])
            elif re.match("cores",uri):
                cores = uri.split('=')[1]
            else:
                uri_list.append((uri.split(',')[0],uri.split(',')[1]))

        routers.append(confCCNHost(name=name , uri_tuples=uri_list, cpu=cpu, cores=cores))

    return routers

def parse_links(conf_arq):
    'Parse links section from the conf file.'
    arq = open(conf_arq,'r')

    links = []

    while True:
        line = arq.readline()
        if line == '[links]\n':
            break

    while True:
        line = arq.readline()
        if line == '':
            break

        args = line.split()
	
	#checks for non-empty line
	if len(args) == 0:
		continue

        h1, h2 = args.pop(0).split(':')

        link_dict = {}

        for arg in args:
            arg_name, arg_value = arg.split('=')
            key = arg_name
            value = arg_value
            if key in ['bw','jitter','max_queue_size']:
                value = int(value)
            if key in ['loss']:
                value = float(value)
            link_dict[key] = value

        links.append(confCCNLink(h1,h2,link_dict))


    return links



