Search in sources :

Example 1 with QueryResultList

use of com.emc.storageos.db.client.constraint.QueryResultList in project coprhd-controller by CoprHD.

the class GeoServiceClient method queryByConstraint.

/**
 * Query the GeoVisible resources by conditions
 *
 * @param constraint the query condition
 * @param result the query result
 * @return list of resources
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public <T> void queryByConstraint(Constraint constraint, QueryResultList<T> result) throws Exception {
    ConstraintDescriptor constrainDescriptor = constraint.toConstraintDescriptor();
    WebResource rRoot = createRequest(GEOVISIBLE_URI + "constraint/" + result.getClass().getName());
    rRoot.accept(MediaType.APPLICATION_OCTET_STREAM);
    ClientResponse resp = addSignature(rRoot).post(ClientResponse.class, constrainDescriptor);
    InputStream input = resp.getEntityInputStream();
    ObjectInputStream objInputStream = new ObjectInputStream(input);
    ResourcesResponse<?> resources = (ResourcesResponse<?>) objInputStream.readObject();
    List<Object> queryResult = (List<Object>) resources.getObjects();
    List<T> ret = new ArrayList<T>();
    for (Object obj : queryResult) {
        ret.add((T) obj);
    }
    result.setResult(ret.iterator());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) WebResource(com.sun.jersey.api.client.WebResource) ResourcesResponse(com.emc.storageos.geomodel.ResourcesResponse) ConstraintDescriptor(com.emc.storageos.db.client.constraint.ConstraintDescriptor) DataObject(com.emc.storageos.db.client.model.DataObject) ArrayList(java.util.ArrayList) QueryResultList(com.emc.storageos.db.client.constraint.QueryResultList) List(java.util.List) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with QueryResultList

use of com.emc.storageos.db.client.constraint.QueryResultList in project coprhd-controller by CoprHD.

the class GeoServiceClient method queryByConstraint.

/**
 * Query the GeoVisible resources by conditions
 *
 * @param constraint the query condition
 * @param result the query result
 * @param startId where the query starts, if it's null, query starts at the beginning
 * @param maxCount the max number of resources returned
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public <T> void queryByConstraint(Constraint constraint, QueryResultList<T> result, URI startId, int maxCount) throws Exception {
    ConstraintDescriptor constrainDescriptor = constraint.toConstraintDescriptor();
    WebResource rRoot = createRequest(GEOVISIBLE_URI + "constraint/" + result.getClass().getName());
    if (startId != null) {
        rRoot = rRoot.queryParam("start_id", startId.toString());
    }
    if (maxCount > 0) {
        rRoot = rRoot.queryParam("max_count", Integer.toString(maxCount));
    }
    rRoot.accept(MediaType.APPLICATION_OCTET_STREAM);
    ClientResponse resp = addSignature(rRoot).post(ClientResponse.class, constrainDescriptor);
    InputStream input = resp.getEntityInputStream();
    ObjectInputStream objInputStream = new ObjectInputStream(input);
    ResourcesResponse<?> resources = (ResourcesResponse<?>) objInputStream.readObject();
    List<Object> queryResult = (List<Object>) resources.getObjects();
    List<T> ret = new ArrayList<T>();
    for (Object obj : queryResult) {
        ret.add((T) obj);
    }
    result.setResult(ret.iterator());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) WebResource(com.sun.jersey.api.client.WebResource) ResourcesResponse(com.emc.storageos.geomodel.ResourcesResponse) ConstraintDescriptor(com.emc.storageos.db.client.constraint.ConstraintDescriptor) DataObject(com.emc.storageos.db.client.model.DataObject) ArrayList(java.util.ArrayList) QueryResultList(com.emc.storageos.db.client.constraint.QueryResultList) List(java.util.List) ObjectInputStream(java.io.ObjectInputStream)

Example 3 with QueryResultList

use of com.emc.storageos.db.client.constraint.QueryResultList in project coprhd-controller by CoprHD.

the class ProjectService method getResourceList.

/**
 * List resources in project
 *
 * @param id the URN of a ViPR Project
 * @prereq none
 * @brief List project resources
 * @return List of resources
 */
@SuppressWarnings("unchecked")
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("/{id}/resources")
@CheckPermission(roles = { Role.SYSTEM_MONITOR, Role.TENANT_ADMIN }, acls = { ACL.ANY })
public ResourceList getResourceList(@PathParam("id") URI id) {
    Project project = getProjectById(id, false);
    QueryResultList<TypedRelatedResourceRep> file = new QueryResultList<TypedRelatedResourceRep>() {

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri) {
            TypedRelatedResourceRep resource = new TypedRelatedResourceRep();
            resource.setId(uri);
            resource.setType(ResourceTypeEnum.FILE);
            return resource;
        }

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri, String name, UUID timestamp) {
            TypedRelatedResourceRep resource = new TypedRelatedResourceRep();
            resource.setId(uri);
            resource.setType(ResourceTypeEnum.FILE);
            resource.setName(name);
            return resource;
        }

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri, Object entry) {
            return createQueryHit(uri);
        }
    };
    QueryResultList<TypedRelatedResourceRep> volume = new QueryResultList<TypedRelatedResourceRep>() {

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri) {
            TypedRelatedResourceRep resource = new TypedRelatedResourceRep();
            resource.setId(uri);
            resource.setType(ResourceTypeEnum.VOLUME);
            return resource;
        }

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri, String name, UUID timestamp) {
            TypedRelatedResourceRep resource = new TypedRelatedResourceRep();
            resource.setId(uri);
            resource.setType(ResourceTypeEnum.VOLUME);
            resource.setName(name);
            return resource;
        }

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri, Object entry) {
            return createQueryHit(uri);
        }
    };
    QueryResultList<TypedRelatedResourceRep> bucket = new QueryResultList<TypedRelatedResourceRep>() {

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri) {
            TypedRelatedResourceRep resource = new TypedRelatedResourceRep();
            resource.setId(uri);
            resource.setType(ResourceTypeEnum.BUCKET);
            return resource;
        }

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri, String name, UUID timestamp) {
            TypedRelatedResourceRep resource = new TypedRelatedResourceRep();
            resource.setId(uri);
            resource.setType(ResourceTypeEnum.BUCKET);
            resource.setName(name);
            return resource;
        }

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri, Object entry) {
            return createQueryHit(uri);
        }
    };
    QueryResultList<TypedRelatedResourceRep> exportgroup = new QueryResultList<TypedRelatedResourceRep>() {

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri) {
            TypedRelatedResourceRep resource = new TypedRelatedResourceRep();
            resource.setId(uri);
            resource.setType(ResourceTypeEnum.EXPORT_GROUP);
            return resource;
        }

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri, String name, UUID timestamp) {
            TypedRelatedResourceRep resource = new TypedRelatedResourceRep();
            resource.setId(uri);
            resource.setType(ResourceTypeEnum.EXPORT_GROUP);
            resource.setName(name);
            return resource;
        }

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri, Object entry) {
            return createQueryHit(uri);
        }
    };
    QueryResultList<TypedRelatedResourceRep> blockSnapshot = new QueryResultList<TypedRelatedResourceRep>() {

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri) {
            TypedRelatedResourceRep resource = new TypedRelatedResourceRep();
            resource.setId(uri);
            resource.setType(ResourceTypeEnum.BLOCK_SNAPSHOT);
            return resource;
        }

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri, String name, UUID timestamp) {
            TypedRelatedResourceRep resource = new TypedRelatedResourceRep();
            resource.setId(uri);
            resource.setType(ResourceTypeEnum.BLOCK_SNAPSHOT);
            resource.setName(name);
            return resource;
        }

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri, Object entry) {
            return createQueryHit(uri);
        }
    };
    QueryResultList<TypedRelatedResourceRep> fileSnapshot = new QueryResultList<TypedRelatedResourceRep>() {

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri) {
            TypedRelatedResourceRep resource = new TypedRelatedResourceRep();
            resource.setId(uri);
            resource.setType(ResourceTypeEnum.FILE_SNAPSHOT);
            return resource;
        }

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri, String name, UUID timestamp) {
            TypedRelatedResourceRep resource = new TypedRelatedResourceRep();
            resource.setId(uri);
            resource.setType(ResourceTypeEnum.FILE_SNAPSHOT);
            resource.setName(name);
            return resource;
        }

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri, Object entry) {
            return createQueryHit(uri);
        }
    };
    QueryResultList<TypedRelatedResourceRep> protectionSet = new QueryResultList<TypedRelatedResourceRep>() {

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri) {
            TypedRelatedResourceRep resource = new TypedRelatedResourceRep();
            resource.setId(uri);
            resource.setType(ResourceTypeEnum.PROTECTION_SET);
            return resource;
        }

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri, String name, UUID timestamp) {
            TypedRelatedResourceRep resource = new TypedRelatedResourceRep();
            resource.setId(uri);
            resource.setType(ResourceTypeEnum.PROTECTION_SET);
            resource.setName(name);
            return resource;
        }

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri, Object entry) {
            return createQueryHit(uri);
        }
    };
    QueryResultList<TypedRelatedResourceRep> blockConsistencySet = new QueryResultList<TypedRelatedResourceRep>() {

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri) {
            TypedRelatedResourceRep resource = new TypedRelatedResourceRep();
            resource.setId(uri);
            resource.setType(ResourceTypeEnum.BLOCK_CONSISTENCY_GROUP);
            return resource;
        }

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri, String name, UUID timestamp) {
            TypedRelatedResourceRep resource = new TypedRelatedResourceRep();
            resource.setId(uri);
            resource.setType(ResourceTypeEnum.BLOCK_CONSISTENCY_GROUP);
            resource.setName(name);
            return resource;
        }

        @Override
        public TypedRelatedResourceRep createQueryHit(URI uri, Object entry) {
            return createQueryHit(uri);
        }
    };
    _dbClient.queryByConstraint(ContainmentConstraint.Factory.getProjectVolumeConstraint(project.getId()), volume);
    _dbClient.queryByConstraint(ContainmentConstraint.Factory.getProjectFileshareConstraint(project.getId()), file);
    _dbClient.queryByConstraint(ContainmentConstraint.Factory.getProjectBucketConstraint(project.getId()), bucket);
    _dbClient.queryByConstraint(ContainmentConstraint.Factory.getProjectExportGroupConstraint(project.getId()), exportgroup);
    _dbClient.queryByConstraint(ContainmentConstraint.Factory.getProjectBlockSnapshotConstraint(project.getId()), blockSnapshot);
    _dbClient.queryByConstraint(ContainmentConstraint.Factory.getProjectFileSnapshotConstraint(project.getId()), fileSnapshot);
    _dbClient.queryByConstraint(ContainmentConstraint.Factory.getProjectProtectionSetConstraint(project.getId()), protectionSet);
    _dbClient.queryByConstraint(ContainmentConstraint.Factory.getProjectBlockConsistencyGroupConstraint(project.getId()), blockConsistencySet);
    ResourceList list = new ResourceList();
    list.setResources(new ChainedList<TypedRelatedResourceRep>(file.iterator(), volume.iterator(), bucket.iterator(), exportgroup.iterator(), blockSnapshot.iterator(), fileSnapshot.iterator(), file.iterator(), volume.iterator(), exportgroup.iterator(), protectionSet.iterator(), blockConsistencySet.iterator()));
    return list;
}
Also used : MapProject(com.emc.storageos.api.mapper.functions.MapProject) Project(com.emc.storageos.db.client.model.Project) TypedRelatedResourceRep(com.emc.storageos.model.TypedRelatedResourceRep) ResourceList(com.emc.storageos.model.project.ResourceList) QueryResultList(com.emc.storageos.db.client.constraint.QueryResultList) URIQueryResultList(com.emc.storageos.db.client.constraint.URIQueryResultList) UUID(java.util.UUID) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) CheckPermission(com.emc.storageos.security.authorization.CheckPermission)

Example 4 with QueryResultList

use of com.emc.storageos.db.client.constraint.QueryResultList in project coprhd-controller by CoprHD.

the class QueryService method queryByConstraint.

@POST
@Path("geo-visible/constraint/{className}")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_OCTET_STREAM })
public Response queryByConstraint(@PathParam("className") String className, ConstraintDescriptor constraintDescriptor, @QueryParam("start_id") URI startId, @QueryParam("max_count") Integer maxCount) throws DatabaseException {
    Constraint condition;
    try {
        condition = constraintDescriptor.toConstraint();
    } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | InstantiationException e) {
        throw APIException.badRequests.invalidParameterWithCause(constraintDescriptor.getClass().getName(), constraintDescriptor.toString(), e);
    }
    try {
        Class clazz = Class.forName(className);
        QueryResultList<?> resultList = (QueryResultList<?>) clazz.newInstance();
        if (maxCount == null) {
            dbClient.queryByConstraint(condition, resultList);
        } else {
            dbClient.queryByConstraint(condition, resultList, startId, maxCount);
        }
        return genResourcesResponse(resultList);
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        log.error("Can't find the class e=", e);
        throw APIException.badRequests.invalidParameter("className", className);
    }
}
Also used : QueryResultList(com.emc.storageos.db.client.constraint.QueryResultList) Constraint(com.emc.storageos.db.client.constraint.Constraint) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

QueryResultList (com.emc.storageos.db.client.constraint.QueryResultList)4 ConstraintDescriptor (com.emc.storageos.db.client.constraint.ConstraintDescriptor)2 DataObject (com.emc.storageos.db.client.model.DataObject)2 ResourcesResponse (com.emc.storageos.geomodel.ResourcesResponse)2 ClientResponse (com.sun.jersey.api.client.ClientResponse)2 WebResource (com.sun.jersey.api.client.WebResource)2 InputStream (java.io.InputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MapProject (com.emc.storageos.api.mapper.functions.MapProject)1 Constraint (com.emc.storageos.db.client.constraint.Constraint)1 URIQueryResultList (com.emc.storageos.db.client.constraint.URIQueryResultList)1 NamedURI (com.emc.storageos.db.client.model.NamedURI)1 Project (com.emc.storageos.db.client.model.Project)1 TypedRelatedResourceRep (com.emc.storageos.model.TypedRelatedResourceRep)1 ResourceList (com.emc.storageos.model.project.ResourceList)1 CheckPermission (com.emc.storageos.security.authorization.CheckPermission)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URI (java.net.URI)1