Search in sources :

Example 1 with SchedulePolicy

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

the class FileDeviceController method assignFileSystemSnapshotPolicy.

@Override
public void assignFileSystemSnapshotPolicy(URI storage, URI fsURI, URI policy, String opId) throws InternalException {
    ControllerUtils.setThreadLocalLogData(fsURI, opId);
    FileDeviceInputOutput args = new FileDeviceInputOutput();
    FileShare fs = null;
    try {
        fs = _dbClient.queryObject(FileShare.class, fsURI);
        SchedulePolicy fp = _dbClient.queryObject(SchedulePolicy.class, policy);
        if (fs != null && fp != null) {
            StorageSystem storageObj = _dbClient.queryObject(StorageSystem.class, storage);
            _log.info("Controller Recieved File Policy  {}", policy);
            args.addFSFileObject(fs);
            args.setFileSystemPath(fs.getPath());
            StoragePool pool = _dbClient.queryObject(StoragePool.class, fs.getPool());
            args.addStoragePool(pool);
            args.addFilePolicy(fp);
            args.setFileOperation(true);
            args.setOpId(opId);
            // Do the Operation on device.
            BiosCommandResult result = getDevice(storageObj.getSystemType()).assignFilePolicy(storageObj, args);
            if (result.isCommandSuccess()) {
                // Update FS database
                StringSet fpolicies = fs.getFilePolicies();
                fpolicies.add(fp.getId().toString());
                fs.setFilePolicies(fpolicies);
                // Update SchedulePolicy database
                StringSet resources = fp.getAssignedResources();
                resources.add(fs.getId().toString());
                fp.setAssignedResources(resources);
            }
            if (result.getCommandPending()) {
                return;
            }
            // Audit & Update the task status
            OperationTypeEnum auditType = null;
            auditType = OperationTypeEnum.ASSIGN_FILE_SYSTEM_SNAPSHOT_SCHEDULE;
            fs.getOpStatus().updateTaskStatus(opId, result.toOperation());
            // Monitoring - Event Processing
            String eventMsg = result.isCommandSuccess() ? "" : result.getMessage();
            recordFileDeviceOperation(_dbClient, auditType, result.isCommandSuccess(), eventMsg, args.getFileSystemPath(), fs, fp);
            _dbClient.updateObject(fs);
            _dbClient.updateObject(fp);
        } else {
            throw DeviceControllerException.exceptions.invalidObjectNull();
        }
    } catch (Exception e) {
        String[] params = { storage.toString(), fsURI.toString(), e.getMessage() };
        _log.error("Unable to assign policy : storage {}, FS URI {},: Error {}", params);
        updateTaskStatus(opId, fs, e);
    }
}
Also used : StoragePool(com.emc.storageos.db.client.model.StoragePool) OperationTypeEnum(com.emc.storageos.services.OperationTypeEnum) StringSet(com.emc.storageos.db.client.model.StringSet) FileDeviceInputOutput(com.emc.storageos.volumecontroller.FileDeviceInputOutput) FileShare(com.emc.storageos.db.client.model.FileShare) SMBFileShare(com.emc.storageos.db.client.model.SMBFileShare) SchedulePolicy(com.emc.storageos.db.client.model.SchedulePolicy) InternalException(com.emc.storageos.svcs.errorhandling.resources.InternalException) ControllerException(com.emc.storageos.volumecontroller.ControllerException) URISyntaxException(java.net.URISyntaxException) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) WorkflowException(com.emc.storageos.workflow.WorkflowException) DatabaseException(com.emc.storageos.db.exceptions.DatabaseException) DeviceControllerException(com.emc.storageos.exceptions.DeviceControllerException) StorageSystem(com.emc.storageos.db.client.model.StorageSystem)

Example 2 with SchedulePolicy

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

the class TenantsService method createPolicy.

/**
 * Worker method for create schedule policy. Allows external requests (REST) as well as
 * internal requests that may not have a security context.
 *
 * @param id the URN of a CoprHD Tenant/Subtenant
 * @param param schedule policy parameters
 * @brief Create schedule policy
 * @return No data returned in response body
 * @throws BadRequestException
 */
public SchedulePolicyResp createPolicy(URI id, PolicyParam param) {
    TenantOrg tenant = getTenantById(id, true);
    // Make policy name as mandatory field
    ArgValidator.checkFieldNotNull(param.getPolicyName(), "policyName");
    // Check for duplicate policy name
    if (param.getPolicyName() != null && !param.getPolicyName().isEmpty()) {
        checkForDuplicateName(param.getPolicyName(), SchedulePolicy.class);
    }
    // check schedule policy type is valid or not
    if (!ArgValidator.isValidEnum(param.getPolicyType(), SchedulePolicyType.class)) {
        throw APIException.badRequests.invalidSchedulePolicyType(param.getPolicyType());
    }
    _log.info("Schedule policy creation started -- ");
    SchedulePolicy schedulePolicy = new SchedulePolicy();
    StringBuilder errorMsg = new StringBuilder();
    // Validate Schedule policy parameters
    boolean isValidSchedule = SchedulePolicyService.validateSchedulePolicyParam(param.getPolicySchedule(), schedulePolicy, errorMsg);
    if (!isValidSchedule && errorMsg != null && errorMsg.length() > 0) {
        _log.error("Failed to create schedule policy due to {} ", errorMsg.toString());
        throw APIException.badRequests.invalidSchedulePolicyParam(param.getPolicyName(), errorMsg.toString());
    }
    // Validate snapshot expire parameters
    boolean isValidSnapshotExpire = false;
    if (param.getSnapshotExpire() != null) {
        // check snapshot expire type is valid or not
        String expireType = param.getSnapshotExpire().getExpireType();
        if (!ArgValidator.isValidEnum(expireType, SnapshotExpireType.class)) {
            _log.error("Invalid schedule snapshot expire type {}. Valid Snapshot expire types are hours, days, weeks, months and never", expireType);
            throw APIException.badRequests.invalidScheduleSnapshotExpireType(expireType);
        }
        isValidSnapshotExpire = SchedulePolicyService.validateSnapshotExpireParam(param.getSnapshotExpire());
        if (!isValidSnapshotExpire) {
            int expireTime = param.getSnapshotExpire().getExpireValue();
            int minExpireTime = 2;
            int maxExpireTime = 10;
            _log.error("Invalid schedule snapshot expire time {}. Try an expire time between {} hours to {} years", expireTime, minExpireTime, maxExpireTime);
            throw APIException.badRequests.invalidScheduleSnapshotExpireValue(expireTime, minExpireTime, maxExpireTime);
        }
    } else {
        if (param.getPolicyType().equalsIgnoreCase(SchedulePolicyType.file_snapshot.toString())) {
            errorMsg.append("Required parameter snapshot_expire was missing or empty");
            _log.error("Failed to create schedule policy due to {} ", errorMsg.toString());
            throw APIException.badRequests.invalidSchedulePolicyParam(param.getPolicyName(), errorMsg.toString());
        }
    }
    if (isValidSchedule) {
        schedulePolicy.setId(URIUtil.createId(SchedulePolicy.class));
        schedulePolicy.setPolicyType(param.getPolicyType());
        schedulePolicy.setLabel(param.getPolicyName());
        schedulePolicy.setPolicyName(param.getPolicyName());
        schedulePolicy.setScheduleFrequency(param.getPolicySchedule().getScheduleFrequency().toLowerCase());
        if (isValidSnapshotExpire) {
            schedulePolicy.setSnapshotExpireType(param.getSnapshotExpire().getExpireType().toLowerCase());
            if (!param.getSnapshotExpire().getExpireType().equalsIgnoreCase(SnapshotExpireType.NEVER.toString())) {
                schedulePolicy.setSnapshotExpireTime((long) param.getSnapshotExpire().getExpireValue());
            }
        }
        schedulePolicy.setTenantOrg(new NamedURI(tenant.getId(), schedulePolicy.getLabel()));
        _dbClient.createObject(schedulePolicy);
        _log.info("Schedule policy {} created successfully", schedulePolicy);
    }
    recordTenantEvent(OperationTypeEnum.CREATE_SCHEDULE_POLICY, tenant.getId(), schedulePolicy.getId());
    return new SchedulePolicyResp(schedulePolicy.getId(), toLink(ResourceTypeEnum.SCHEDULE_POLICY, schedulePolicy.getId()), schedulePolicy.getLabel());
}
Also used : SchedulePolicyResp(com.emc.storageos.model.schedulepolicy.SchedulePolicyResp) NamedURI(com.emc.storageos.db.client.model.NamedURI) TenantOrg(com.emc.storageos.db.client.model.TenantOrg) SchedulePolicyType(com.emc.storageos.db.client.model.SchedulePolicy.SchedulePolicyType) SchedulePolicy(com.emc.storageos.db.client.model.SchedulePolicy) AlternateIdConstraint(com.emc.storageos.db.client.constraint.AlternateIdConstraint) ContainmentConstraint(com.emc.storageos.db.client.constraint.ContainmentConstraint) ContainmentPermissionsConstraint(com.emc.storageos.db.client.constraint.ContainmentPermissionsConstraint) Constraint(com.emc.storageos.db.client.constraint.Constraint) SnapshotExpireType(com.emc.storageos.db.client.model.SchedulePolicy.SnapshotExpireType)

Example 3 with SchedulePolicy

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

the class TenantsService method getSchedulePolicies.

/**
 * Gets the policyIds, policyNames and self links for all schedule policies.
 *
 * @param id the URN of a CoprHD Tenant/Subtenant
 * @brief List schedule policies
 * @return policyList - A SchedulePolicyList reference specifying the policyIds, name and self links for
 *         the schedule policies.
 */
@GET
@Path("/{id}/schedule-policies")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@CheckPermission(roles = { Role.TENANT_ADMIN, Role.PROJECT_ADMIN })
public SchedulePolicyList getSchedulePolicies(@PathParam("id") URI id) {
    TenantOrg tenant = getTenantById(id, false);
    StorageOSUser user = getUserFromContext();
    NamedElementQueryResultList schedulePolicies = new NamedElementQueryResultList();
    if (_permissionsHelper.userHasGivenRole(user, tenant.getId(), Role.SYSTEM_MONITOR, Role.TENANT_ADMIN, Role.SECURITY_ADMIN)) {
        // list all schedule policies
        _dbClient.queryByConstraint(ContainmentConstraint.Factory.getTenantOrgSchedulePolicyConstraint(tenant.getId()), schedulePolicies);
    } else {
        // list only schedule policies that the user has access to
        if (!id.equals(URI.create(user.getTenantId()))) {
            throw APIException.forbidden.insufficientPermissionsForUser(user.getName());
        }
        Map<URI, Set<String>> allMySchedulePolicies = _permissionsHelper.getAllPermissionsForUser(user, tenant.getId(), null, false);
        if (!allMySchedulePolicies.keySet().isEmpty()) {
            List<SchedulePolicy> policyList = _dbClient.queryObjectField(SchedulePolicy.class, "label", new ArrayList<URI>(allMySchedulePolicies.keySet()));
            List<NamedElementQueryResultList.NamedElement> elements = new ArrayList<NamedElementQueryResultList.NamedElement>(policyList.size());
            for (SchedulePolicy policy : policyList) {
                elements.add(NamedElementQueryResultList.NamedElement.createElement(policy.getId(), policy.getLabel()));
            }
            schedulePolicies.setResult(elements.iterator());
        } else {
            // empty list
            schedulePolicies.setResult(new ArrayList<NamedElementQueryResultList.NamedElement>().iterator());
        }
    }
    SchedulePolicyList policyList = new SchedulePolicyList();
    for (NamedElementQueryResultList.NamedElement el : schedulePolicies) {
        policyList.getSchdulePolicies().add(toNamedRelatedResource(ResourceTypeEnum.SCHEDULE_POLICY, el.getId(), el.getName()));
    }
    return policyList;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) AbstractChangeTrackingSet(com.emc.storageos.db.client.model.AbstractChangeTrackingSet) StringSet(com.emc.storageos.db.client.model.StringSet) ArrayList(java.util.ArrayList) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI) SchedulePolicyList(com.emc.storageos.model.schedulepolicy.SchedulePolicyList) StorageOSUser(com.emc.storageos.security.authentication.StorageOSUser) TenantOrg(com.emc.storageos.db.client.model.TenantOrg) NamedElementQueryResultList(com.emc.storageos.db.client.constraint.NamedElementQueryResultList) SchedulePolicy(com.emc.storageos.db.client.model.SchedulePolicy) 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 SchedulePolicy

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

the class SchedulePolicyService method deleteSchedulePolicy.

/**
 * Delete schedule policy from CoprHD DB.
 *
 * @param policyId the URN of a schedule policy
 * @brief Delete schedule policy from CoprHD DB
 * @return No data returned in response body
 * @throws BadRequestException
 */
@DELETE
@Path("/{id}")
@CheckPermission(roles = { Role.TENANT_ADMIN })
public Response deleteSchedulePolicy(@PathParam("id") URI policyId) {
    ArgValidator.checkFieldUriType(policyId, SchedulePolicy.class, "policyId");
    SchedulePolicy schedulePolicy = getPolicyById(policyId, true);
    ArgValidator.checkReference(SchedulePolicy.class, policyId, checkForDelete(schedulePolicy));
    // Check policy is associated with FS before delete
    StringSet resources = schedulePolicy.getAssignedResources();
    if (resources != null && !resources.isEmpty()) {
        _log.error("Unable to delete schedule policy {} as it is already associated with resources", schedulePolicy.getPolicyName());
        throw APIException.badRequests.unableToDeleteSchedulePolicy(schedulePolicy.getPolicyName());
    }
    _dbClient.markForDeletion(schedulePolicy);
    _log.info("Schedule policy {} deleted successfully", schedulePolicy.getPolicyName());
    return Response.ok().build();
}
Also used : StringSet(com.emc.storageos.db.client.model.StringSet) MapSchedulePolicy(com.emc.storageos.api.mapper.functions.MapSchedulePolicy) SchedulePolicy(com.emc.storageos.db.client.model.SchedulePolicy) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) CheckPermission(com.emc.storageos.security.authorization.CheckPermission)

Example 5 with SchedulePolicy

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

the class IsilonFileStorageDevice method assignFilePolicy.

@Deprecated
@Override
public BiosCommandResult assignFilePolicy(StorageSystem storage, FileDeviceInputOutput args) {
    // for isilon we need to create a new policy for each individual file system
    SchedulePolicy fp = args.getFilePolicy();
    String snapshotScheduleName = fp.getPolicyName() + "_" + args.getFsName();
    String pattern = snapshotScheduleName + "_%Y-%m-%d_%H-%M";
    String Schedulevalue = getIsilonScheduleString(fp);
    Integer expireValue = getSnapshotExpireValue(fp);
    _log.info("File Policy  name : {}", snapshotScheduleName);
    IsilonApi isi = getIsilonDevice(storage);
    try {
        isi.createSnapshotSchedule(snapshotScheduleName, args.getFileSystemPath(), Schedulevalue, pattern, expireValue);
    } catch (IsilonException e) {
        _log.error("assign file policy failed.", e);
        return BiosCommandResult.createErrorResult(e);
    }
    return BiosCommandResult.createSuccessfulResult();
}
Also used : IsilonApi(com.emc.storageos.isilon.restapi.IsilonApi) SchedulePolicy(com.emc.storageos.db.client.model.SchedulePolicy) IsilonException(com.emc.storageos.isilon.restapi.IsilonException)

Aggregations

SchedulePolicy (com.emc.storageos.db.client.model.SchedulePolicy)14 StringSet (com.emc.storageos.db.client.model.StringSet)8 FileShare (com.emc.storageos.db.client.model.FileShare)5 CheckPermission (com.emc.storageos.security.authorization.CheckPermission)5 Path (javax.ws.rs.Path)5 MapSchedulePolicy (com.emc.storageos.api.mapper.functions.MapSchedulePolicy)4 NamedURI (com.emc.storageos.db.client.model.NamedURI)4 SMBFileShare (com.emc.storageos.db.client.model.SMBFileShare)4 StorageSystem (com.emc.storageos.db.client.model.StorageSystem)4 URI (java.net.URI)4 Produces (javax.ws.rs.Produces)4 ArrayList (java.util.ArrayList)3 GET (javax.ws.rs.GET)3 SchedulePolicyType (com.emc.storageos.db.client.model.SchedulePolicy.SchedulePolicyType)2 SnapshotExpireType (com.emc.storageos.db.client.model.SchedulePolicy.SnapshotExpireType)2 StoragePool (com.emc.storageos.db.client.model.StoragePool)2 TenantOrg (com.emc.storageos.db.client.model.TenantOrg)2 DatabaseException (com.emc.storageos.db.exceptions.DatabaseException)2 DeviceControllerException (com.emc.storageos.exceptions.DeviceControllerException)2 IsilonApi (com.emc.storageos.isilon.restapi.IsilonApi)2