Search in sources :

Example 11 with DataObject

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

the class BlockSnapshotService method deactivateSnapshot.

/**
 * Deactivate volume snapshot, will result in permanent deletion of the requested snapshot from the storage system it was created on
 * and will move the snapshot to a "marked-for-delete" state after the deletion happens on the array side.
 * It will be deleted by the garbage collector on a subsequent iteration
 * If this snapshot was created from a volume that is part of a consistency group,
 * then all the related snapshots will be deactivated, as well.
 *
 * If "?type=VIPR_ONLY" is added to the path, it will delete snapshot only from ViPR data base and leaves the snapshot on storage array
 * as it is.
 * Possible value for attribute type : FULL, VIPR_ONLY
 * FULL : Deletes the snapshot permanently on array and ViPR data base.
 * VIPR_ONLY : Deletes the snapshot only from ViPR data base and leaves the snapshot on array as it is.
 *
 * @prereq none
 * @param id the URN of a ViPR snapshot
 * @param type the type of deletion {@link DefaultValue} FULL
 * @brief Delete snapshot
 * @return Snapshot information
 */
@POST
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("/{id}/deactivate")
@CheckPermission(roles = { Role.TENANT_ADMIN }, acls = { ACL.ANY })
public TaskList deactivateSnapshot(@PathParam("id") URI id, @DefaultValue("FULL") @QueryParam("type") String type) {
    _log.info("Executing {} snapshot delete for snapshot {}", type, id);
    String opStage = null;
    boolean successStatus = true;
    String taskId = UUID.randomUUID().toString();
    TaskList response = new TaskList();
    // Get the snapshot.
    BlockSnapshot snap = (BlockSnapshot) queryResource(id);
    // We can ignore dependencies on BlockSnapshotSession. In this case
    // the BlockSnapshot instance is a linked target for a BlockSnapshotSession
    // and we will unlink the snapshot from the session and delete it.
    List<Class<? extends DataObject>> excludeTypes = new ArrayList<Class<? extends DataObject>>();
    excludeTypes.add(BlockSnapshotSession.class);
    if (VolumeDeleteTypeEnum.VIPR_ONLY.name().equals(type)) {
        excludeTypes.add(ExportGroup.class);
        excludeTypes.add(ExportMask.class);
    }
    ArgValidator.checkReference(BlockSnapshot.class, id, checkForDelete(snap, excludeTypes));
    if (!VolumeDeleteTypeEnum.VIPR_ONLY.name().equals(type)) {
        // The audit log message operation stage.
        opStage = AuditLogManager.AUDITOP_BEGIN;
        // If the BlockSnapshot instance represents a linked target, then
        // we need to unlink the target form the snapshot session and then
        // delete the target.
        URIQueryResultList snapSessionURIs = new URIQueryResultList();
        _dbClient.queryByConstraint(ContainmentConstraint.Factory.getLinkedTargetSnapshotSessionConstraint(id), snapSessionURIs);
        Iterator<URI> snapSessionURIsIter = snapSessionURIs.iterator();
        if (snapSessionURIsIter.hasNext()) {
            _log.info("Snapshot is linked target for a snapshot session");
            SnapshotSessionUnlinkTargetsParam param = new SnapshotSessionUnlinkTargetsParam();
            List<SnapshotSessionUnlinkTargetParam> targetInfoList = new ArrayList<SnapshotSessionUnlinkTargetParam>();
            SnapshotSessionUnlinkTargetParam targetInfo = new SnapshotSessionUnlinkTargetParam(id, Boolean.TRUE);
            targetInfoList.add(targetInfo);
            param.setLinkedTargets(targetInfoList);
            response.getTaskList().add(getSnapshotSessionManager().unlinkTargetVolumesFromSnapshotSession(snapSessionURIsIter.next(), param, OperationTypeEnum.DELETE_VOLUME_SNAPSHOT));
            return response;
        }
        // Not an error if the snapshot we try to delete is already deleted
        if (snap.getInactive()) {
            _log.info("Snapshot is already inactive");
            Operation op = new Operation();
            op.ready("The snapshot has already been deleted");
            op.setResourceType(ResourceOperationTypeEnum.DELETE_VOLUME_SNAPSHOT);
            _dbClient.createTaskOpStatus(BlockSnapshot.class, snap.getId(), taskId, op);
            response.getTaskList().add(toTask(snap, taskId, op));
            return response;
        }
    }
    // Get the storage system.
    StorageSystem device = _dbClient.queryObject(StorageSystem.class, snap.getStorageController());
    // Determine all snapshots to delete.
    List<BlockSnapshot> snapshots = new ArrayList<BlockSnapshot>();
    final URI cgId = snap.getConsistencyGroup();
    if (!NullColumnValueGetter.isNullURI(cgId) && !NullColumnValueGetter.isNullValue(snap.getReplicationGroupInstance())) {
        // Collect all the BlockSnapshots if part of a CG.
        snapshots = ControllerUtils.getSnapshotsPartOfReplicationGroup(snap, _dbClient);
    } else {
        // Snap is not part of a CG so only delete the snap
        snapshots.add(snap);
    }
    // Get the snapshot parent volume.
    Volume parentVolume = _permissionsHelper.getObjectById(snap.getParent(), Volume.class);
    // Check that there are no pending tasks for these snapshots.
    checkForPendingTasks(Arrays.asList(parentVolume.getTenant().getURI()), snapshots);
    // Create tasks on the volume.
    for (BlockSnapshot snapshot : snapshots) {
        Operation snapOp = _dbClient.createTaskOpStatus(BlockSnapshot.class, snapshot.getId(), taskId, ResourceOperationTypeEnum.DELETE_VOLUME_SNAPSHOT);
        response.getTaskList().add(toTask(snapshot, taskId, snapOp));
    }
    // should be returned.
    try {
        BlockServiceApi blockServiceApiImpl = BlockService.getBlockServiceImpl(parentVolume, _dbClient);
        blockServiceApiImpl.deleteSnapshot(snap, snapshots, taskId, type);
    } catch (APIException | InternalException e) {
        successStatus = false;
        String errorMsg = String.format("Exception attempting to delete snapshot %s: %s", snap.getId(), e.getMessage());
        _log.error(errorMsg);
        for (TaskResourceRep taskResourceRep : response.getTaskList()) {
            taskResourceRep.setState(Operation.Status.error.name());
            taskResourceRep.setMessage(errorMsg);
            _dbClient.error(BlockSnapshot.class, taskResourceRep.getResource().getId(), taskId, e);
        }
    } catch (Exception e) {
        successStatus = false;
        String errorMsg = String.format("Exception attempting to delete snapshot %s: %s", snap.getId(), e.getMessage());
        _log.error(errorMsg);
        ServiceCoded sc = APIException.internalServerErrors.genericApisvcError(errorMsg, e);
        for (TaskResourceRep taskResourceRep : response.getTaskList()) {
            taskResourceRep.setState(Operation.Status.error.name());
            taskResourceRep.setMessage(sc.getMessage());
            _dbClient.error(BlockSnapshot.class, taskResourceRep.getResource().getId(), taskId, sc);
        }
    }
    auditOp(OperationTypeEnum.DELETE_VOLUME_SNAPSHOT, successStatus, opStage, id.toString(), snap.getLabel(), snap.getParent().getName(), device.getId().toString());
    return response;
}
Also used : TaskList(com.emc.storageos.model.TaskList) ArrayList(java.util.ArrayList) Operation(com.emc.storageos.db.client.model.Operation) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI) URIQueryResultList(com.emc.storageos.db.client.constraint.URIQueryResultList) SnapshotSessionUnlinkTargetsParam(com.emc.storageos.model.block.SnapshotSessionUnlinkTargetsParam) StorageSystem(com.emc.storageos.db.client.model.StorageSystem) MapBlockSnapshot(com.emc.storageos.api.mapper.functions.MapBlockSnapshot) BlockSnapshot(com.emc.storageos.db.client.model.BlockSnapshot) TaskResourceRep(com.emc.storageos.model.TaskResourceRep) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) InternalException(com.emc.storageos.svcs.errorhandling.resources.InternalException) ServiceCodeException(com.emc.storageos.svcs.errorhandling.resources.ServiceCodeException) InternalException(com.emc.storageos.svcs.errorhandling.resources.InternalException) DataObject(com.emc.storageos.db.client.model.DataObject) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) SnapshotSessionUnlinkTargetParam(com.emc.storageos.model.block.SnapshotSessionUnlinkTargetParam) Volume(com.emc.storageos.db.client.model.Volume) ServiceCoded(com.emc.storageos.svcs.errorhandling.model.ServiceCoded) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) CheckPermission(com.emc.storageos.security.authorization.CheckPermission)

Example 12 with DataObject

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

the class ArgValidatorTest method testCheckEntityInactiveEntityBadRequest.

@Test(expected = BadRequestException.class)
public void testCheckEntityInactiveEntityBadRequest() {
    try {
        DataObject object = new DataObject() {
        };
        object.setId(URI.create("urn:storageos:StorageSystem:2b91947d-749f-4356-aad7-dcd7f7906197:vdc1"));
        object.setInactive(true);
        ArgValidator.checkEntity(object, object.getId(), false);
    } catch (APIException bre) {
        assertEquals(ServiceCode.API_PARAMETER_INACTIVE, bre.getServiceCode());
        assertEquals("Entity with the given id urn:storageos:StorageSystem:2b91947d-749f-4356-aad7-dcd7f7906197:vdc1 is inactive and marked for deletion", bre.getLocalizedMessage());
        throw bre;
    }
}
Also used : DataObject(com.emc.storageos.db.client.model.DataObject) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) Test(org.junit.Test)

Example 13 with DataObject

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

the class ArgValidatorTest method testCheckEntityInactiveEntityNotfound.

@Test(expected = NotFoundException.class)
public void testCheckEntityInactiveEntityNotfound() {
    try {
        DataObject object = new DataObject() {
        };
        object.setId(URI.create("urn:storageos:StorageSystem:2b91947d-749f-4356-aad7-dcd7f7906197:vdc1"));
        object.setInactive(true);
        ArgValidator.checkEntity(object, object.getId(), true);
    } catch (APIException bre) {
        assertEquals(ServiceCode.API_URL_ENTITY_INACTIVE, bre.getServiceCode());
        assertEquals("Entity specified in URL with the given id urn:storageos:StorageSystem:2b91947d-749f-4356-aad7-dcd7f7906197:vdc1 is inactive and marked for deletion", bre.getLocalizedMessage());
        throw bre;
    }
}
Also used : DataObject(com.emc.storageos.db.client.model.DataObject) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) Test(org.junit.Test)

Example 14 with DataObject

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

the class DummyDBClient method queryObject.

@Override
public <T extends DataObject> List<T> queryObject(Class<T> clazz, URI... id) throws DatabaseException {
    checkStarted();
    List<T> objectList = new ArrayList<T>();
    for (URI idEntry : id) {
        Object entry = _idToObjectMap.get(idEntry);
        if (null != entry) {
            objectList.add((T) entry);
        }
    }
    return objectList;
}
Also used : ArrayList(java.util.ArrayList) DataObject(com.emc.storageos.db.client.model.DataObject) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI)

Example 15 with DataObject

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

the class ComputeSystemHelper method listChildren.

/**
 * This function is to retrieve the children of a given class.
 *
 * @param id the URN of parent
 * @param clzz the child class
 * @param nameField the name of the field of the child class that will be displayed as
 *            name in {@link NamedRelatedResourceRep}. Note this field should be a required
 *            field because, objects for which this field is null will not be returned by
 *            this function.
 * @param linkField the name of the field in the child class that stored the parent id
 * @return a list of children of tenant for the given class
 */
protected static <T extends DataObject> List<NamedElementQueryResultList.NamedElement> listChildren(DbClient dbClient, URI id, Class<T> clzz, String nameField, String linkField) {
    @SuppressWarnings("deprecation") List<URI> uris = dbClient.queryByConstraint(ContainmentConstraint.Factory.getContainedObjectsConstraint(id, clzz, linkField));
    if (uris != null && !uris.isEmpty()) {
        Iterator<T> dataObjects = dbClient.queryIterativeObjectField(clzz, nameField, uris);
        List<NamedElementQueryResultList.NamedElement> elements = new ArrayList<NamedElementQueryResultList.NamedElement>();
        while (dataObjects.hasNext()) {
            T dataObject = dataObjects.next();
            Object name = DataObjectUtils.getPropertyValue(clzz, dataObject, nameField);
            elements.add(NamedElementQueryResultList.NamedElement.createElement(dataObject.getId(), name == null ? "" : name.toString()));
        }
        return elements;
    } else {
        return new ArrayList<NamedElementQueryResultList.NamedElement>();
    }
}
Also used : ArrayList(java.util.ArrayList) BlockObject(com.emc.storageos.db.client.model.BlockObject) DataObject(com.emc.storageos.db.client.model.DataObject) DiscoveredDataObject(com.emc.storageos.db.client.model.DiscoveredDataObject) URI(java.net.URI) NamedElement(com.emc.storageos.db.client.constraint.NamedElementQueryResultList.NamedElement) NamedElementQueryResultList(com.emc.storageos.db.client.constraint.NamedElementQueryResultList)

Aggregations

DataObject (com.emc.storageos.db.client.model.DataObject)154 URI (java.net.URI)62 ArrayList (java.util.ArrayList)53 DiscoveredDataObject (com.emc.storageos.db.client.model.DiscoveredDataObject)44 URIQueryResultList (com.emc.storageos.db.client.constraint.URIQueryResultList)30 Volume (com.emc.storageos.db.client.model.Volume)26 NamedURI (com.emc.storageos.db.client.model.NamedURI)24 StringSet (com.emc.storageos.db.client.model.StringSet)23 HashMap (java.util.HashMap)22 BlockObject (com.emc.storageos.db.client.model.BlockObject)17 HashSet (java.util.HashSet)17 BlockSnapshot (com.emc.storageos.db.client.model.BlockSnapshot)16 UnManagedVolume (com.emc.storageos.db.client.model.UnManagedDiscoveredObjects.UnManagedVolume)14 Operation (com.emc.storageos.db.client.model.Operation)13 List (java.util.List)10 Set (java.util.Set)10 BlockSnapshotSession (com.emc.storageos.db.client.model.BlockSnapshotSession)9 TaskResourceRep (com.emc.storageos.model.TaskResourceRep)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 BlockConsistencyGroup (com.emc.storageos.db.client.model.BlockConsistencyGroup)8