Search in sources :

Example 1 with CustomServicesDBResource

use of com.emc.storageos.db.client.model.uimodels.CustomServicesDBResource in project coprhd-controller by CoprHD.

the class CustomServicesDBHelper method update.

/**
 * Given an ID, update param, DB column family, and primitive type save a new primitive to the DB
 * and return the primitive type java object
 *
 * @param mapper The primitive mapper
 * @param dbModel The DB column family class
 * @param resourceType The resource
 * @param primitiveManager The database access component
 * @param client The model client
 * @param param The primitive update param
 * @param id The id of the primitive to update
 *
 * @return The primitive type java object
 */
public static <DBModel extends CustomServicesDBPrimitive, T extends CustomServicesDBPrimitiveType> T update(final Function<DBModel, T> mapper, final Class<DBModel> dbModel, final Class<? extends CustomServicesDBResource> resourceType, final CustomServicesPrimitiveManager primitiveManager, final ModelClient client, final CustomServicesPrimitiveUpdateParam param, final Function<UpdatePrimitive<DBModel>, StringSetMap> updateInputFunc, final Function<UpdatePrimitive<DBModel>, StringMap> updateAttributesFunc, final URI id, final Class<? extends CustomServicesDBResource> referencedByresourceType) {
    final DBModel primitive = primitiveManager.findById(dbModel, id);
    if (null == primitive) {
        throw APIException.notFound.unableToFindEntityInURL(id);
    } else if (primitive.getInactive()) {
        throw APIException.notFound.entityInURLIsInactive(id);
    }
    checkNotInUse(client, id, primitive);
    if (StringUtils.isNotBlank(param.getName())) {
        final String label = param.getName().trim();
        if (!label.equalsIgnoreCase(primitive.getLabel())) {
            checkDuplicateLabel(label, primitiveManager, dbModel);
            primitive.setLabel(label);
        }
    }
    if (null != param.getFriendlyName()) {
        primitive.setFriendlyName(param.getFriendlyName());
    }
    if (null != param.getDescription()) {
        primitive.setDescription(param.getDescription());
    }
    final NamedURI oldResourceId = primitive.getResource();
    final CustomServicesDBResource resource;
    if (param.getResource() == null) {
        resource = null;
    } else if (!resourceType.isAssignableFrom(CustomServicesDBNoResource.class)) {
        resource = primitiveManager.findResource(resourceType, param.getResource());
        if (null == resource) {
            throw APIException.notFound.unableToFindEntityInURL(param.getResource());
        }
        if (resource.getInactive()) {
            throw APIException.notFound.entityInURLIsInactive(param.getResource());
        }
        primitive.setResource(new NamedURI(resource.getId(), resource.getLabel()));
    } else {
        throw APIException.badRequests.invalidParameter("resource", param.getResource().toString());
    }
    final UpdatePrimitive<DBModel> updatePrimitive = new UpdatePrimitive<DBModel>(param, primitive);
    updateAttributesFunc.apply(updatePrimitive);
    updateInputFunc.apply(updatePrimitive);
    updateOutput(param.getOutput(), primitive);
    client.save(primitive);
    // check that the resource is not referenced by other primitives
    if (null != resource && null != oldResourceId && null == checkResourceNotReferenced(dbModel, CustomServicesDBPrimitive.RESOURCE, client, oldResourceId.getURI(), resource)) {
        // Find all the associated inventory files if exist for the old resource and delete the associated inventory files if exist
        deleteReferencedInventoryResources(oldResourceId.getURI(), primitiveManager, client, referencedByresourceType);
        // delete old resource (after updating the inventory, if any exists)
        client.delete(primitiveManager.findResource(resourceType, oldResourceId.getURI()));
    }
    return mapper.apply(primitive);
}
Also used : CustomServicesDBResource(com.emc.storageos.db.client.model.uimodels.CustomServicesDBResource) NamedURI(com.emc.storageos.db.client.model.NamedURI)

Example 2 with CustomServicesDBResource

use of com.emc.storageos.db.client.model.uimodels.CustomServicesDBResource in project coprhd-controller by CoprHD.

the class CustomServicesDBHelper method updateResource.

/**
 * Given a resource ID and parameters update a resource in the database
 *
 * @param dbModel The database column family of the resource
 * @param primitiveManager The database access component
 * @param id The ID of the resource
 * @param name The new name of the resource null, if no update
 * @param stream The new bytes of the resource, null if no update
 * @param attributes The new attributes of the resource, null if no update
 * @return The updated java object instance of this resource type
 */
public static <T extends CustomServicesDBResourceType<?>> T updateResource(final Class<T> type, final Class<? extends CustomServicesDBResource> dbModel, final CustomServicesPrimitiveManager primitiveManager, final URI id, final String name, final byte[] stream, final StringSetMap attributes, final URI parentId, final ModelClient client, final Class<? extends CustomServicesDBResource> referencedByResource, final String referencedByResourceColumnName, final Class<? extends CustomServicesDBPrimitive> referencedByPrimitive, final String referencedByPrimitiveColumnName) {
    final CustomServicesDBResource resource = primitiveManager.findResource(dbModel, id);
    if (null == resource) {
        throw APIException.notFound.unableToFindEntityInURL(id);
    } else if (resource.getInactive()) {
        throw APIException.notFound.entityInURLIsInactive(id);
    }
    if (StringUtils.isNotBlank(name)) {
        final String label = name.trim();
        if (!label.equalsIgnoreCase(resource.getLabel())) {
            checkDuplicateLabel(label, primitiveManager, dbModel);
            resource.setLabel(label);
        }
    }
    if (null != parentId) {
        resource.setParentId(parentId);
    }
    if (null != attributes || null != stream) {
        BadRequestException resourceReferencedexception = checkResourceNotReferenced(referencedByPrimitive, referencedByPrimitiveColumnName, client, id, resource);
        if (resourceReferencedexception != null) {
            throw resourceReferencedexception;
        }
        resourceReferencedexception = checkResourceNotReferenced(referencedByResource, referencedByResourceColumnName, client, id, resource);
        if (resourceReferencedexception != null) {
            throw resourceReferencedexception;
        }
        resource.setAttributes(attributes);
        resource.setResource(Base64.encodeBase64(stream));
    }
    primitiveManager.save(resource);
    return makeResourceType(type, resource);
}
Also used : CustomServicesDBResource(com.emc.storageos.db.client.model.uimodels.CustomServicesDBResource) BadRequestException(com.emc.storageos.svcs.errorhandling.resources.BadRequestException)

Example 3 with CustomServicesDBResource

use of com.emc.storageos.db.client.model.uimodels.CustomServicesDBResource in project coprhd-controller by CoprHD.

the class CustomServicesDBHelper method deactivateResource.

/**
 * Deactivate a resource database instance
 *
 * @param dbModel The database column family class
 * @param primitiveManager The database access component
 * @param client The model client
 * @param id ID of the resource to deactivate
 */
public static void deactivateResource(final Class<? extends CustomServicesDBResource> dbModel, final CustomServicesPrimitiveManager primitiveManager, final ModelClient client, final URI id, final Class<? extends CustomServicesDBResource> referencedByResource, final String referencedByResourceColumnName, final Class<? extends CustomServicesDBPrimitive> referencedByPrimitive, final String referencedByPrimitiveColumnName) {
    final CustomServicesDBResource resource = primitiveManager.findResource(dbModel, id);
    if (null == resource) {
        throw APIException.notFound.unableToFindEntityInURL(id);
    }
    BadRequestException resourceReferencedexception = checkResourceNotReferenced(referencedByPrimitive, referencedByPrimitiveColumnName, client, id, resource);
    if (resourceReferencedexception != null) {
        throw resourceReferencedexception;
    }
    resourceReferencedexception = checkResourceNotReferenced(referencedByResource, referencedByResourceColumnName, client, id, resource);
    if (resourceReferencedexception != null) {
        throw resourceReferencedexception;
    }
    client.delete(resource);
}
Also used : CustomServicesDBResource(com.emc.storageos.db.client.model.uimodels.CustomServicesDBResource) BadRequestException(com.emc.storageos.svcs.errorhandling.resources.BadRequestException)

Example 4 with CustomServicesDBResource

use of com.emc.storageos.db.client.model.uimodels.CustomServicesDBResource in project coprhd-controller by CoprHD.

the class CustomServicesDBHelper method deleteReferencedInventoryResources.

/**
 * Delete the inventory files assciated with the resource files
 *
 * @param resourceId The resourceId to search for inventory files
 * @param primitiveManager The database access component
 * @param client The model client
 * @param referencedByresourceType The DB component which is the inventory resource to delete
 */
private static void deleteReferencedInventoryResources(final URI resourceId, final CustomServicesPrimitiveManager primitiveManager, final ModelClient client, final Class<? extends CustomServicesDBResource> referencedByresourceType) {
    if (null != resourceId && null != referencedByresourceType) {
        final List<NamedElement> refResource = listResources(referencedByresourceType, client, CustomServicesDBResource.PARENTID, resourceId.toString());
        for (final NamedElement eachResource : refResource) {
            final CustomServicesDBResource refDBResource = primitiveManager.findResource(referencedByresourceType, eachResource.getId());
            // delete the associated inventory files if exist for the DB model
            client.delete(refDBResource);
        }
    }
}
Also used : CustomServicesDBResource(com.emc.storageos.db.client.model.uimodels.CustomServicesDBResource) NamedElement(com.emc.storageos.db.client.constraint.NamedElementQueryResultList.NamedElement)

Example 5 with CustomServicesDBResource

use of com.emc.storageos.db.client.model.uimodels.CustomServicesDBResource in project coprhd-controller by CoprHD.

the class CustomServicesDBHelper method create.

/**
 * Given a creation param, DB column family, and primitive type save a new primitive to the DB
 * and return the primitive type java object
 *
 * @param mapper The primitive mapper
 * @param dbModel The database column family java class
 * @param resourceType The resource
 * @param primitiveManager The database access component
 * @param param The primitive creation param
 * @return The primitive type java object
 */
public static <DBModel extends CustomServicesDBPrimitive, T extends CustomServicesDBPrimitiveType> T create(final Function<DBModel, T> mapper, final Class<DBModel> dbModel, final Class<? extends CustomServicesDBResource> resourceType, final CustomServicesPrimitiveManager primitiveManager, final Function<CustomServicesPrimitiveCreateParam, StringSetMap> createInputFunc, final Function<CustomServicesPrimitiveCreateParam, StringMap> createAttributesFunc, final CustomServicesPrimitiveCreateParam param) {
    final DBModel primitive;
    try {
        primitive = dbModel.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new RuntimeException("Failed to create custom services primitive: " + dbModel.getSimpleName());
    }
    primitive.setId(URIUtil.createId(primitive.getClass()));
    if (StringUtils.isNotBlank(param.getName())) {
        checkDuplicateLabel(param.getName().trim(), primitiveManager, dbModel);
        primitive.setLabel(param.getName().trim());
    } else {
        throw APIException.badRequests.requiredParameterMissingOrEmpty("name");
    }
    primitive.setFriendlyName(param.getFriendlyName());
    primitive.setDescription(param.getDescription());
    if (!resourceType.isAssignableFrom(CustomServicesDBNoResource.class)) {
        if (param.getResource() == null) {
            throw APIException.badRequests.requiredParameterMissingOrEmpty("resource");
        }
        final CustomServicesDBResource resource = primitiveManager.findResource(resourceType, param.getResource());
        if (null == resource) {
            throw APIException.notFound.unableToFindEntityInURL(param.getResource());
        }
        if (resource.getInactive()) {
            throw APIException.notFound.entityInURLIsInactive(param.getResource());
        }
        primitive.setResource(new NamedURI(resource.getId(), resource.getLabel()));
    } else if (null != param.getResource()) {
        throw APIException.badRequests.invalidParameter("resource", param.getResource().toString());
    }
    primitive.setAttributes(createAttributesFunc.apply(param));
    primitive.setInput(createInputFunc.apply(param));
    primitive.setOutput(createOutput(param.getOutput()));
    primitiveManager.save(primitive);
    return mapper.apply(primitive);
}
Also used : CustomServicesDBResource(com.emc.storageos.db.client.model.uimodels.CustomServicesDBResource) NamedURI(com.emc.storageos.db.client.model.NamedURI)

Aggregations

CustomServicesDBResource (com.emc.storageos.db.client.model.uimodels.CustomServicesDBResource)7 NamedURI (com.emc.storageos.db.client.model.NamedURI)2 BadRequestException (com.emc.storageos.svcs.errorhandling.resources.BadRequestException)2 NamedElement (com.emc.storageos.db.client.constraint.NamedElementQueryResultList.NamedElement)1 CustomServicesDBPrimitive (com.emc.storageos.db.client.model.uimodels.CustomServicesDBPrimitive)1