'Create networks based on parameters.'

def _do_create_networks(self, context, label, cidr, multi_host, num_networks, network_size, cidr_v6, gateway, gateway_v6, bridge, bridge_interface, dns1=None, dns2=None, fixed_cidr=None, mtu=None, dhcp_server=None, enable_dhcp=None, share_address=None, allowed_start=None, allowed_end=None, **kwargs):

    fixed_net_v4 = netaddr.IPNetwork('0/32')
    fixed_net_v6 = netaddr.IPNetwork('::0/128')
    subnets_v4 = []
    subnets_v6 = []
    if kwargs.get('ipam'):
        if cidr_v6:
            subnets_v6 = [netaddr.IPNetwork(cidr_v6)]
        if cidr:
            subnets_v4 = [netaddr.IPNetwork(cidr)]
    else:
        subnet_bits = int(math.ceil(math.log(network_size, 2)))
        if cidr_v6:
            fixed_net_v6 = netaddr.IPNetwork(cidr_v6)
            prefixlen_v6 = (128 - subnet_bits)
            if (prefixlen_v6 > 64):
                prefixlen_v6 = 64
            subnets_v6 = fixed_net_v6.subnet(prefixlen_v6, count=num_networks)
        if cidr:
            fixed_net_v4 = netaddr.IPNetwork(cidr)
            prefixlen_v4 = (32 - subnet_bits)
            subnets_v4 = list(fixed_net_v4.subnet(prefixlen_v4, count=num_networks))
    if cidr:
        try:
            nets = objects.NetworkList.get_all(context)
        except exception.NoNetworksFound:
            nets = []
        num_used_nets = len(nets)
        self._validate_cidr(context, nets, subnets_v4, fixed_net_v4)
    networks = objects.NetworkList(context=context, objects=[])
    subnets = six.moves.zip_longest(subnets_v4, subnets_v6)
    for (index, (subnet_v4, subnet_v6)) in enumerate(subnets):
        net = objects.Network(context=context)
        uuid = kwargs.get('uuid')
        if uuid:
            net.uuid = uuid
        net.bridge = bridge
        net.bridge_interface = bridge_interface
        net.multi_host = multi_host
        net.dns1 = dns1
        net.dns2 = dns2
        net.mtu = mtu
        net.enable_dhcp = enable_dhcp
        net.share_address = share_address
        net.project_id = kwargs.get('project_id')
        if (num_networks > 1):
            net.label = ('%s_%d' % (label, index))
        else:
            net.label = label
        bottom_reserved = self._bottom_reserved_ips
        top_reserved = self._top_reserved_ips
        extra_reserved = []
        if (cidr and subnet_v4):
            current = subnet_v4[1]
            if allowed_start:
                val = self._index_of(subnet_v4, allowed_start)
                current = netaddr.IPAddress(allowed_start)
                bottom_reserved = val
            if allowed_end:
                val = self._index_of(subnet_v4, allowed_end)
                top_reserved = ((subnet_v4.size - 1) - val)
            net.cidr = str(subnet_v4)
            net.netmask = str(subnet_v4.netmask)
            net.broadcast = str(subnet_v4.broadcast)
            if gateway:
                net.gateway = gateway
            else:
                net.gateway = current
                current += 1
            net.dhcp_server = (dhcp_server or net.gateway)
            net.dhcp_start = current
            current += 1
            if (net.dhcp_start == net.dhcp_server):
                net.dhcp_start = current
            extra_reserved.append(str(net.dhcp_server))
            extra_reserved.append(str(net.gateway))
        if (cidr_v6 and subnet_v6):
            net.cidr_v6 = str(subnet_v6)
            if gateway_v6:
                net.gateway_v6 = str(gateway_v6)
            else:
                net.gateway_v6 = str(subnet_v6[1])
            net.netmask_v6 = str(subnet_v6.netmask)
        if (CONF.network_manager == 'nova.network.manager.VlanManager'):
            vlan = kwargs.get('vlan', None)
            if (not vlan):
                index_vlan = (index + num_used_nets)
                vlan = (kwargs['vlan_start'] + index_vlan)
                used_vlans = [x.vlan for x in nets]
                if (vlan in used_vlans):
                    used_vlans.sort()
                    vlan = (used_vlans[(-1)] + 1)
            net.vpn_private_address = net.dhcp_start
            extra_reserved.append(str(net.vpn_private_address))
            net.dhcp_start = (net.dhcp_start + 1)
            net.vlan = vlan
            net.bridge = ('br%s' % vlan)
            index_vpn = (index + num_used_nets)
            net.vpn_public_port = (kwargs['vpn_start'] + index_vpn)
        net.create()
        networks.objects.append(net)
        if (cidr and subnet_v4):
            self._create_fixed_ips(context, net.id, fixed_cidr, extra_reserved, bottom_reserved, top_reserved)
    return obj_base.obj_to_primitive(networks)
