All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openstack4j.openstack.internal.BaseOpenStackService Maven / Gradle / Ivy

There is a newer version: 3.12
Show newest version
package org.openstack4j.openstack.internal;

import java.util.*;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import org.openstack4j.api.client.CloudProvider;
import org.openstack4j.api.exceptions.OS4JException;
import org.openstack4j.api.types.ServiceType;
import org.openstack4j.core.transport.*;
import org.openstack4j.core.transport.HttpRequest.RequestBuilder;
import org.openstack4j.core.transport.internal.HttpExecutor;
import org.openstack4j.model.ModelEntity;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.common.Payload;
import org.openstack4j.model.identity.AuthVersion;
import org.openstack4j.model.identity.v2.Access;
import org.openstack4j.model.identity.v3.Service;

import static org.openstack4j.core.transport.ClientConstants.HEADER_USER_AGENT;
import static org.openstack4j.core.transport.ClientConstants.USER_AGENT;

public class BaseOpenStackService {

    private static ThreadLocal reqIdContainer = new ThreadLocal<>();
    private ServiceType serviceType = ServiceType.IDENTITY;
    private Function endpointFunc;

    protected BaseOpenStackService() {
    }

    protected BaseOpenStackService(ServiceType serviceType) {
        this(serviceType, null);
    }

    protected BaseOpenStackService(ServiceType serviceType, Function endpointFunc) {
        this.serviceType = serviceType;
        this.endpointFunc = endpointFunc;
    }

    public String getXOpenstackRequestId() {
        return reqIdContainer.get();
    }

    protected  Invocation get(Class returnType, String... path) {
        return builder(returnType, path, HttpMethod.GET);
    }

    protected Invocation getWithResponse(String... path) {
        return builder(ActionResponse.class, path, HttpMethod.GET);
    }

    protected  Invocation post(Class returnType, String... path) {
        return builder(returnType, path, HttpMethod.POST);
    }

    protected Invocation postWithResponse(String... path) {
        return builder(ActionResponse.class, path, HttpMethod.POST);
    }

    protected  Invocation put(Class returnType, String... path) {
        return builder(returnType, path, HttpMethod.PUT);
    }

    protected Invocation putWithResponse(String... path) {
        return builder(ActionResponse.class, path, HttpMethod.PUT);
    }

    protected  Invocation patch(Class returnType, String... path) {
        return builder(returnType, path, HttpMethod.PATCH);
    }

    protected Invocation patchWithResponse(String... path) {
        return builder(ActionResponse.class, path, HttpMethod.PATCH);
    }

    protected  Invocation delete(Class returnType, String... path) {
        return builder(returnType, path, HttpMethod.DELETE);
    }

    protected Invocation deleteWithResponse(String... path) {
        return builder(ActionResponse.class, path, HttpMethod.DELETE);
    }

    protected  Invocation head(Class returnType, String... path) {
        return builder(returnType, path, HttpMethod.HEAD);
    }

    protected  Invocation request(HttpMethod method, Class returnType, String path) {
        return builder(returnType, path, method);
    }

    protected String uri(String path, Object... params) {
        if (params.length == 0)
            return path;
        return String.format(path, params);
    }

    private  Invocation builder(Class returnType, String[] path, HttpMethod method) {
        return builder(returnType, Joiner.on("").join(path), method);
    }

    private  Invocation builder(Class returnType, String path, HttpMethod method) {
        OSClientSession ses = OSClientSession.getCurrent();
        if (ses == null) {
            throw new OS4JException(
                    "Unable to retrieve current session. Please verify thread has a current session available.");
        }
        RequestBuilder req = HttpRequest.builder(returnType).endpointTokenProvider(ses).config(ses.getConfig())
                .method(method).path(path);
        Map headers = ses.getHeaders();
        if (headers != null && headers.size() > 0) {
            return new Invocation<>(req, serviceType, endpointFunc).headers(headers);
        } else {
            return new Invocation<>(req, serviceType, endpointFunc);
        }
    }

    protected int getServiceVersion() {
        OSClientSession session = OSClientSession.getCurrent();
        if (session.getAuthVersion() == AuthVersion.V3) {
            SortedSet services = ((OSClientSession.OSClientSessionV3) session).getToken().getAggregatedCatalog().get(serviceType.getType());
            Service service = ((OSClientSession.OSClientSessionV3) session).getToken().getAggregatedCatalog().get(serviceType.getType()).first();

            if (services.isEmpty()) {
                return 1;
            }

            return service.getVersion();

        } else {
            SortedSet services = ((OSClientSession.OSClientSessionV2) session).getAccess().getAggregatedCatalog().get(serviceType.getType());
            Access.Service service = ((OSClientSession.OSClientSessionV2) session).getAccess().getAggregatedCatalog().get(serviceType.getType()).first();

            if (services.isEmpty()) {
                return 1;
            }

            return service.getVersion();
        }

    }

    protected  List toList(T[] arr) {
        if (arr == null)
            return Collections.emptyList();
        return Arrays.asList(arr);
    }

    protected CloudProvider getProvider() {
        return OSClientSession.getCurrent().getProvider();
    }

    protected static class Invocation {
        RequestBuilder req;

        protected Invocation(RequestBuilder req, ServiceType serviceType, Function endpointFunc) {
            this.req = req;
            req.serviceType(serviceType);
            req.endpointFunction(endpointFunc);
        }

        public HttpRequest getRequest() {
            return req.build();
        }

        public Invocation param(String name, Object value) {
            req.queryParam(name, value);
            return this;
        }

        public Invocation updateParam(String name, Object value) {
            req.updateQueryParam(name, value);
            return this;
        }

        public Invocation params(Map params) {
            if (params != null) {
                for (String name : params.keySet())
                    req.queryParam(name, params.get(name));
            }
            return this;
        }

        public Invocation param(boolean condition, String name, Object value) {
            if (condition)
                req.queryParam(name, value);
            return this;
        }

        public Invocation paramLists(Map> params) {
            if (params != null) {
                for (Map.Entry> pair : params.entrySet())
                    for (Object value : pair.getValue())
                        req.queryParam(pair.getKey(), value);
            }
            return this;
        }

        public Invocation serviceType(ServiceType serviceType) {
            req.serviceType(serviceType);
            return this;
        }

        public Invocation entity(ModelEntity entity) {
            req.entity(entity);
            return this;
        }

        public Invocation entity(Payload entity) {
            req.entity(entity);
            req.contentType(ClientConstants.CONTENT_TYPE_OCTECT_STREAM);
            return this;
        }

        public Invocation contentType(String contentType) {
            req.contentType(contentType);
            return this;
        }

        public Invocation json(String json) {
            req.json(json);
            return this;
        }

        public Invocation headers(Map headers) {
            if (headers != null)
                req.headers(headers);
            return this;
        }

        public Invocation header(String name, Object value) {
            req.header(name, value);
            return this;
        }

        public R execute() {
            return execute(null);
        }

        public R execute(ExecutionOptions options) {
            header(HEADER_USER_AGENT, USER_AGENT);
            HttpRequest request = req.build();
            HttpResponse res = HttpExecutor.create().execute(request);

            reqIdContainer.remove();
            reqIdContainer.set(getRequestId(res));
            return res.getEntity(request.getReturnType(), options);
        }

        public HttpResponse executeWithResponse() {
            HttpResponse res = HttpExecutor.create().execute(req.build());
            reqIdContainer.remove();
            reqIdContainer.set(getRequestId(res));
            return res;
        }

        private String getRequestId(HttpResponse res) {
            if (res.headers().containsKey(ClientConstants.X_COMPUTE_REQUEST_ID)) {
                return res.header(ClientConstants.X_COMPUTE_REQUEST_ID);
            } else {
                return res.header(ClientConstants.X_OPENSTACK_REQUEST_ID);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy