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

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

package org.openstack4j.openstack.internal;

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

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;

import org.openstack4j.api.client.CloudProvider;
import org.openstack4j.api.exceptions.OS4JException;
import org.openstack4j.api.types.ServiceType;
import org.openstack4j.core.transport.ClientConstants;
import org.openstack4j.core.transport.ExecutionOptions;
import org.openstack4j.core.transport.HttpMethod;
import org.openstack4j.core.transport.HttpRequest;
import org.openstack4j.core.transport.HttpRequest.RequestBuilder;
import org.openstack4j.core.transport.HttpResponse;
import org.openstack4j.core.transport.internal.HttpExecutor;
import org.openstack4j.model.ModelEntity;
import org.openstack4j.model.common.Payload;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.identity.Access.Service;

import com.google.common.base.Function;
import com.google.common.base.Joiner;

public class BaseOpenStackService {



    ServiceType serviceType = ServiceType.IDENTITY;
    Function endpointFunc;

    protected BaseOpenStackService() {
    }

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

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

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

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

    protected  Invocation put(Class returnType, String... path) {
        return builder(returnType, path, HttpMethod.PUT);
    }
    
    protected  Invocation patch(Class returnType, String... path) {
        return builder(returnType, 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);
        return new Invocation(req, serviceType, endpointFunc);
    }

    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();
            return HttpExecutor.create().execute(request).getEntity(request.getReturnType(), options);
        }

        public HttpResponse executeWithResponse() {
            return HttpExecutor.create().execute(req.build());
        }

    }
    
    protected int getServiceVersion() {
        OSClientSession session = OSClientSession.getCurrent();
        SortedSet services = session.getAccess().getAggregatedCatalog().get(serviceType.getTypeV3());
        System.out.println(services);
        if (services.isEmpty()) {
            return 1;
        }
        
        Service service = session.getAccess().getAggregatedCatalog().get(serviceType.getTypeV3()).first();
        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();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy