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

com.emc.vipr.client.core.AbstractResources Maven / Gradle / Ivy

There is a newer version: 3.5.0.0
Show newest version
/*
 * Copyright (c) 2015 EMC Corporation
 * All Rights Reserved
 */
package com.emc.vipr.client.core;

import static com.emc.vipr.client.core.util.ResourceUtils.refIds;
import static com.emc.vipr.client.core.util.ResourceUtils.defaultList;
import static com.emc.vipr.client.core.impl.PathConstants.*;
import java.net.URI;
import java.util.*;
import com.emc.storageos.model.DataObjectRestRep;
import com.emc.storageos.model.RelatedResourceRep;
import com.emc.storageos.model.TagAssignment;
import com.emc.storageos.model.TaskList;
import com.emc.storageos.model.TaskResourceRep;
import com.emc.storageos.model.auth.*;
import com.emc.storageos.model.quota.QuotaInfo;
import com.emc.storageos.model.quota.QuotaUpdateParam;
import com.emc.storageos.model.search.SearchResultResourceRep;
import com.emc.storageos.model.search.SearchResults;
import com.emc.storageos.model.search.Tags;
import com.emc.vipr.client.Task;
import com.emc.vipr.client.Tasks;
import com.emc.vipr.client.core.filters.ResourceFilter;
import com.emc.vipr.client.impl.RestClient;
import com.emc.vipr.client.core.search.SearchBuilder;
import com.emc.vipr.client.core.util.ResourceUtils;
import javax.ws.rs.core.UriBuilder;

/**
 * Base class for all resource types. This class provides the implementation of many methods that may or may not be
 * implemented by all resources as a convenience (ACLs/Tasks/Quotas). This also provides a basic implementation of
 * {@link Resources#getByIds(Collection, ResourceFilter)} that looks up each resource in the collection one by one.
 * 
 * @param 
 *            the type of resource.
 */
public abstract class AbstractResources implements Resources {

    protected final RestClient client;
    protected final Class resourceClass;
    protected final String baseUrl;

    /** Whether to include inactive resources in fetch operations, defaults to false. */
    private boolean includeInactive;

    /** Whether to include internal resources in fetch operations, defaults to false. */
    private boolean includeInternal;

    public AbstractResources(RestClient client, Class resourceClass, String baseUrl) {
        this.client = client;
        this.baseUrl = baseUrl;
        this.resourceClass = resourceClass;
    }

    /**
     * Configures the fetch operations to include inactive resources.
     * 
     * @param inactive
     *            whether to include inactive resources.
     * @return this AbstractResources.
     */
    public AbstractResources withInactive(boolean inactive) {
        this.includeInactive = inactive;
        return this;
    }

    /**
     * Configures the fetch operations to include internal resources.
     * 
     * @param internal
     *            whether to include internal resources.
     * @return this AbstractResources.
     */
    public AbstractResources withInternal(boolean internal) {
        this.includeInternal = internal;
        return this;
    }

    /**
     * Gets the URL for selecting a resource by ID: baseUrl/{id}
     * 
     * @return the ID URL.
     */
    protected String getIdUrl() {
        return String.format(ID_URL_FORMAT, baseUrl);
    }

    /**
     * Gets the URL for deactivating a resource by ID: baseUrl/{id}/deactivate
     * 
     * @return the deactivate URL.
     */
    protected String getDeactivateUrl() {
        return String.format(DEACTIVATE_URL_FORMAT, baseUrl);
    }

    /**
     * Gets the URL for registering a resource by ID: baseUrl/{id}/register
     * 
     * @return the deactivate URL.
     */
    protected String getRegisterUrl() {
        return String.format(REGISTER_URL_FORMAT, baseUrl);
    }

    /**
     * Gets the URL for de-registering a resource by ID: baseUrl/{id}/deregister
     * 
     * @return the deactivate URL.
     */
    protected String getDeregisterUrl() {
        return String.format(DEREGISTER_URL_FORMAT, baseUrl);
    }

    /**
     * Gets the URL for getting/setting tags for a resource by ID: baseUrl/{id}/tags
     * 
     * @return the tags URL.
     */
    protected String getTagsUrl() {
        return String.format(TAGS_URL_FORMAT, baseUrl);
    }

    /**
     * Gets the URL for getting/setting ACLs for a resource by ID: baseUrl/{id}/acl
     * 
     * @return the ACLs URL.
     */
    protected String getAclUrl() {
        return String.format(ACL_URL_FORMAT, baseUrl);
    }

    /**
     * Gets the URL for getting/setting role assignments for a resource by ID: baseUrl/{id}/role-assignments
     * 
     * @return the role assignments URL.
     */
    protected String getRoleAssignmentsUrl() {
        return String.format(ID_URL_FORMAT + ROLE_ASSIGNMENT_PATH, baseUrl);
    }

    /**
     * Gets the URL for getting/setting quotas for a resource by ID: baseUrl/{id}/quota
     * 
     * @return the quota URL.
     */
    protected String getQuotaUrl() {
        return String.format(QUOTA_URL_FORMAT, baseUrl);
    }

    /**
     * Gets the URL for searching for resources of this type: baseUrl/search
     * 
     * @return the search URL.
     */
    protected String getSearchUrl() {
        return String.format(SEARCH_URL_FORMAT, baseUrl);
    }

    @Override
    public T get(URI id) {
        if (id != null) {
            return client.get(resourceClass, getIdUrl(), id);
        }
        else {
            return null;
        }
    }

    @Override
    public T get(RelatedResourceRep ref) {
        return (ref != null) ? get(ref.getId()) : null;
    }

    @Override
    public List getByIds(Collection ids) {
        return getByIds(ids, null);
    }

    @Override
    public List getByIds(Collection ids, ResourceFilter filter) {
        List results = new ArrayList();
        if (ids != null) {
            for (URI id : ids) {
                if (!acceptId(id, filter)) {
                    continue;
                }

                T item = get(id);
                if ((item != null) && accept(item, filter)) {
                    results.add(item);
                }
            }
        }
        return results;
    }

    @Override
    public List getByRefs(Collection resources) {
        return getByRefs(resources, null);
    }

    @Override
    public List getByRefs(Collection refs, ResourceFilter filter) {
        return getByIds(refIds(refs), filter);
    }

    @Override
    public Set getTags(URI id) {
        Tags tags = client.get(Tags.class, getTagsUrl(), id);
        return tags != null ? tags.getTag() : new HashSet();
    }

    @Override
    public void addTags(URI id, Set add) {
        updateTags(id, new TagAssignment(add, new HashSet()));
    }

    @Override
    public void removeTags(URI id, Set remove) {
        updateTags(id, new TagAssignment(new HashSet(), remove));
    }

    @Override
    public void updateTags(URI id, TagAssignment tags) {
        client.put(String.class, tags, getTagsUrl(), id);
    }

    /**
     * Deactivates a resource by ID. Some resource types return no result on deactivate.
     * 
     * @param id
     *            the ID of the resource to deactivate.
     */
    protected void doDeactivate(URI id) {
        client.post(String.class, getDeactivateUrl(), id);
    }

    /**
     * Deactivates a resource by ID and returns the deactivate task. Some resource types return a task when
     * deactivating.
     * 
     * @param id
     *            the ID of the resource to deactivate.
     * @return the deactivate task.
     */
    protected Task doDeactivateWithTask(URI id) {
        return postTask(getDeactivateUrl(), id);
    }

    /**
     * Deactivates a resource by ID and returns the deactivate tasks. Some resource types return tasks when
     * deactivating.
     * 
     * @param id
     *            the ID of the resource to deactivate.
     * @return the deactivate tasks.
     */
    protected Tasks doDeactivateWithTasks(URI id) {
        return postTasks(getDeactivateUrl(), id);
    }

    /**
     * Gets the tasks associated with a resource by ID (when supported).
     * 

* API Call: GET baseUrl/{id}/tasks * * @param id * the ID of the resource. * @return the tasks associated with the resource. */ protected Tasks doGetTasks(URI id) { TaskList response = client.get(TaskList.class, getIdUrl() + "/tasks", id); return new Tasks(client, response.getTaskList(), resourceClass); } /** * Gets a single task for a resource by ID. *

* API Call: GET baseUrl/{id}/tasks/{taskId} * * @param id * the ID of the resource. * @param taskId * the ID of the task to retrieve. * @return the task. */ protected Task doGetTask(URI id, URI taskId) { TaskResourceRep response = client.get(TaskResourceRep.class, getIdUrl() + "/tasks/{taskId}", id, taskId); return new Task(client, response, resourceClass); } /** * Get ACLs for resources that support it. *

* API Call: GET baseUrl/{id}/acl * * @param id * the ID of the resource. * @return the list of ACL entries. */ protected List doGetACLs(URI id) { ACLAssignments response = client.get(ACLAssignments.class, getAclUrl(), id); return defaultList(response.getAssignments()); } /** * Update ACLs for resource that support it. *

* API Call: PUT baseUrl/{id}/acl * * @param id * the ID of the resource. * @param aclChanges * the ACL changes. * @return the resulting list of ACL entries. */ protected List doUpdateACLs(URI id, ACLAssignmentChanges aclChanges) { ACLAssignments response = client.put(ACLAssignments.class, aclChanges, getAclUrl(), id); return defaultList(response.getAssignments()); } /** * Gets the quota info for resources that support it. *

* API Call: GET baseUrl/{id}/quota * * @param id * the ID of the resource. * @return the quota info. */ protected QuotaInfo doGetQuota(URI id) { return client.get(QuotaInfo.class, getQuotaUrl(), id); } /** * Updates the quota info for resources that support it. *

* API Call: PUT baseUrl/{id}/quota * * @param id * the ID of the resource. * @param quota * the quota update information. * @return the quota info. */ protected QuotaInfo doUpdateQuota(URI id, QuotaUpdateParam quota) { return client.put(QuotaInfo.class, quota, getQuotaUrl(), id); } /** * Performs a POST with no request that will return a single task as a response. * * @param path * the path to post to. * @param args * the path arguments. * @return the task object. */ protected Task postTask(String path, Object... args) { TaskResourceRep task = client.post(TaskResourceRep.class, path, args); return new Task(client, task, resourceClass); } /** * Performs a POST with a request that will return a single task as a response. * * @param request * the request object. * @param path * the path to post to. * @param args * the path arguments. * @return the task object. */ protected Task postTask(Object request, String path, Object... args) { TaskResourceRep task = client.post(TaskResourceRep.class, request, path, args); return new Task(client, task, resourceClass); } /** * Performs a POST with no request and returns a single task as a response. * * @param uri * the URI to post to. * @return the task object. */ protected Task postTaskURI(URI uri) { TaskResourceRep task = client.postURI(TaskResourceRep.class, uri); return new Task(client, task, resourceClass); } /** * Performs a DELETE with no request and returns a single task as a response. * * @param uri * the URI to post to. * @return the task object. */ protected Task deleteTaskURI(URI uri) { TaskResourceRep task = client.deleteURI(TaskResourceRep.class, uri); return new Task(client, task, resourceClass); } /** * Performs a DELETE with a request that will return a single task as a response. * * @param path * the path to post to. * @param args * the path arguments. * @return the task object. */ protected Task deleteTask(String path, Object... args) { TaskResourceRep task = client.delete(TaskResourceRep.class, path, args); return new Task(client, task, resourceClass); } /** * Performs a POST with a request that will return a single task as a response. * * @param request * the request object. * @param uri * the URI to post to. * @return the task object. */ protected Task postTaskURI(Object request, URI uri) { TaskResourceRep task = client.postURI(TaskResourceRep.class, request, uri); return new Task(client, task, resourceClass); } /** * Performs a PUT with a request that will return a single task as a response. * * @param request * the request object. * @param path * the path to put to. * @param args * the path arguments. * @return the task object. */ protected Task putTask(Object request, String path, Object... args) { TaskResourceRep task = client.put(TaskResourceRep.class, request, path, args); return new Task(client, task, resourceClass); } /** * Performs a PUT with a request that will return a single task as a response. * * @param request * the request object. * @param uri * the URI to put to. * @return the task object. */ protected Task putTaskURI(Object request, URI uri) { TaskResourceRep task = client.putURI(TaskResourceRep.class, request, uri); return new Task(client, task, resourceClass); } /** * Performs a POST with no request that will return multiple tasks as a response. * * @param path * the path to post to. * @param args * the path arguments. * @return the tasks object. */ protected Tasks postTasks(String path, Object... args) { TaskList tasks = client.post(TaskList.class, path, args); return new Tasks(client, tasks.getTaskList(), resourceClass); } /** * Performs a POST with a request that will return multiple tasks as a response. * * @param request * the request object. * @param path * the path to post to. * @param args * the path arguments. * @return the tasks object. */ protected Tasks postTasks(Object request, String path, Object... args) { TaskList tasks = client.post(TaskList.class, request, path, args); return new Tasks(client, tasks.getTaskList(), resourceClass); } /** * Performs a POST with a request that will return multiple tasks as a response. * * @param request * the request object. * @param uri * the URI to post to. * @return the tasks object. */ protected Tasks postTasksURI(Object request, URI uri) { TaskList tasks = client.postURI(TaskList.class, request, uri); return new Tasks(client, tasks.getTaskList(), resourceClass); } /** * Determines if the ID is accepted by the filter. * * @param id * the ID. * @param filter * the filter (may be null for no filtering). * @return true if the ID is accepted. */ protected boolean acceptId(URI id, ResourceFilter filter) { if (id == null) { return false; } if (filter != null) { return filter.acceptId(id); } return true; } /** * Determines if the item is accepted. * * @param item * the item. * @param filter * the filter (may be null for no filtering). * @return true if the item is accepted. */ protected boolean accept(V item, ResourceFilter filter) { // Filter inactive if (!includeInactive && !ResourceUtils.isActive(item)) { return false; } // Filter internal if (!includeInternal && !ResourceUtils.isNotInternal(item)) { return false; } if (filter != null) { return filter.accept(item); } return true; } @Override public SearchBuilder search() { return new SearchBuilder(this); } /** * Performs a search for resources matching the a single search parameter. * * @param name * the parameter name. * @param value * the parameter value. * @return the list of resources. */ public List performSearchBy(String name, Object value) { Map params = Collections.singletonMap(name, value); return performSearch(params); } /** * Performs a search for resources matching the given parameters. * * @param params * the search query parameters. * @return the list of resources. */ public List performSearch(Map params) { UriBuilder builder = client.uriBuilder(getSearchUrl()); for (Map.Entry entry : params.entrySet()) { builder.queryParam(entry.getKey(), entry.getValue()); } SearchResults searchResults = client.getURI(SearchResults.class, builder.build()); List results = searchResults.getResource(); if (results == null) { results = new ArrayList(); } return results; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy