'Retrieve and format the current V3 service catalog.
:param user_id: The id of the user who has been authenticated for
creating service catalog.
:param project_id: The id of the project. \'project_id\' will be None in
the case this being called to create a catalog to go in a domain
scoped token. In this case, any endpoint that requires a
project_id as part of their URL will be skipped.
:returns: A list representing the service catalog or an empty list'

def get_v3_catalog(self, user_id, project_id):

    d = dict(itertools.chain(CONF.items(), CONF.eventlet_server.items()))
    d.update({'user_id': user_id})
    silent_keyerror_failures = []
    if project_id:
        d.update({'tenant_id': project_id, 'project_id': project_id})
    else:
        silent_keyerror_failures = ['tenant_id', 'project_id']
    with sql.session_for_read() as session:
        services = session.query(Service).filter((Service.enabled == true())).options(sql.joinedload(Service.endpoints)).all()
        def make_v3_endpoints(endpoints):
            for endpoint in (ep.to_dict() for ep in endpoints if ep.enabled):
                del endpoint['service_id']
                del endpoint['legacy_endpoint_id']
                del endpoint['enabled']
                endpoint['region'] = endpoint['region_id']
                try:
                    formatted_url = utils.format_url(endpoint['url'], d, silent_keyerror_failures=silent_keyerror_failures)
                    if formatted_url:
                        endpoint['url'] = formatted_url
                    else:
                        continue
                except exception.MalformedEndpoint:
                    continue
                (yield endpoint)
        def make_v3_service(svc):
            eps = list(make_v3_endpoints(svc.endpoints))
            service = {'endpoints': eps, 'id': svc.id, 'type': svc.type}
            service['name'] = svc.extra.get('name', '')
            return service
        catalog_ref = [make_v3_service(svc) for svc in services]
        filtered_endpoints = {}
        if project_id:
            filtered_endpoints = self.catalog_api.list_endpoints_for_project(project_id)
        if filtered_endpoints:
            filtered_ids = list(filtered_endpoints.keys())
            for service in catalog_ref[:]:
                endpoints = service['endpoints']
                for endpoint in endpoints[:]:
                    endpoint_id = endpoint['id']
                    if (endpoint_id not in filtered_ids):
                        service['endpoints'].remove(endpoint)
                        continue
                    if (not filtered_endpoints[endpoint_id]['enabled']):
                        service['endpoints'].remove(endpoint)
                if (not service.get('endpoints')):
                    catalog_ref.remove(service)
        elif (not CONF.endpoint_filter.return_all_endpoints_if_no_filter):
            return []
        return catalog_ref
