'convert a network info vif to injectable instance data.'

def _vif_xenstore_data(self, vif):

    def get_ip(ip):
        if (not ip):
            return None
        return ip['address']
    def fixed_ip_dict(ip, subnet):
        if (ip['version'] == 4):
            netmask = str(subnet.as_netaddr().netmask)
        else:
            netmask = subnet.as_netaddr()._prefixlen
        return {'ip': ip['address'], 'enabled': '1', 'netmask': netmask, 'gateway': get_ip(subnet['gateway'])}
    def convert_route(route):
        return {'route': str(netaddr.IPNetwork(route['cidr']).network), 'netmask': str(netaddr.IPNetwork(route['cidr']).netmask), 'gateway': get_ip(route['gateway'])}
    network = vif['network']
    v4_subnets = [subnet for subnet in network['subnets'] if (subnet['version'] == 4)]
    v6_subnets = [subnet for subnet in network['subnets'] if (subnet['version'] == 6)]
    routes = [convert_route(route) for subnet in network['subnets'] for route in subnet['routes']]
    dns = [get_ip(ip) for subnet in network['subnets'] for ip in subnet['dns']]
    info_dict = {'label': network['label'], 'mac': vif['address']}
    if v4_subnets:
        info_dict['gateway'] = get_ip(v4_subnets[0]['gateway'])
        info_dict['broadcast'] = str(v4_subnets[0].as_netaddr().broadcast)
        info_dict['ips'] = [fixed_ip_dict(ip, subnet) for subnet in v4_subnets for ip in subnet['ips']]
    if v6_subnets:
        info_dict['gateway_v6'] = get_ip(v6_subnets[0]['gateway'])
        info_dict['ip6s'] = [fixed_ip_dict(ip, subnet) for subnet in v6_subnets for ip in subnet['ips']]
    if routes:
        info_dict['routes'] = routes
    if dns:
        info_dict['dns'] = list(set(dns))
    return info_dict
