Search in sources :

Example 36 with FileExportRule

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

the class FileDeviceController method export.

@Override
public void export(URI storage, URI uri, List<FileShareExport> exports, String opId) throws ControllerException {
    ControllerUtils.setThreadLocalLogData(uri, opId);
    FileObject fsObj = null;
    FileShare fs = null;
    Snapshot snapshotObj = null;
    StorageSystem storageObj = null;
    try {
        storageObj = _dbClient.queryObject(StorageSystem.class, storage);
        FileDeviceInputOutput args = new FileDeviceInputOutput();
        boolean isFile = false;
        if (URIUtil.isType(uri, FileShare.class)) {
            isFile = true;
            fs = _dbClient.queryObject(FileShare.class, uri);
            fsObj = fs;
            args.addFSFileObject(fs);
            StoragePool pool = _dbClient.queryObject(StoragePool.class, fs.getPool());
            args.addStoragePool(pool);
            setVirtualNASinArgs(fs.getVirtualNAS(), args);
        } else {
            snapshotObj = _dbClient.queryObject(Snapshot.class, uri);
            fsObj = snapshotObj;
            fs = _dbClient.queryObject(FileShare.class, snapshotObj.getParent());
            args.addFileShare(fs);
            args.addSnapshotFileObject(snapshotObj);
            StoragePool pool = _dbClient.queryObject(StoragePool.class, fs.getPool());
            args.addStoragePool(pool);
            setVirtualNASinArgs(fs.getVirtualNAS(), args);
        }
        args.setFileOperation(isFile);
        args.setOpId(opId);
        _log.info("Export details...  ");
        List<FileExport> fileExports = new ArrayList<FileExport>();
        if (exports != null) {
            for (FileShareExport fileShareExport : exports) {
                args.setBypassDnsCheck(fileShareExport.getBypassDnsCheck());
                FileExport fExport = fileShareExport.getFileExport();
                fExport.setMountPoint(fileShareExport.getMountPath());
                _log.info("FileExport:clients:" + fExport.getClients() + ":portName:" + fExport.getStoragePortName() + ":port:" + fExport.getStoragePort() + ":rootMapping:" + fExport.getRootUserMapping() + ":permissions:" + fExport.getPermissions() + ":protocol:" + fExport.getProtocol() + ":security:" + fExport.getSecurityType() + ":mountpoint:" + fExport.getMountPoint() + ":path:" + fExport.getPath() + ":comments:" + fExport.getComments() + ":subDirectory:" + fExport.getSubDirectory());
                fileExports.add(fExport);
            }
        } else {
            _log.info("Exports are null");
        }
        // Acquire lock for VNXFILE Storage System
        acquireStepLock(storageObj, opId);
        WorkflowStepCompleter.stepExecuting(opId);
        BiosCommandResult result = getDevice(storageObj.getSystemType()).doExport(storageObj, args, fileExports);
        if (result.getCommandPending()) {
            return;
        }
        if (!result.isCommandSuccess() && !result.getCommandPending()) {
            WorkflowStepCompleter.stepFailed(opId, result.getServiceCoded());
        }
        // Set Mount path info for the exports
        FSExportMap fsExports = fsObj.getFsExports();
        // Per New model get the rules and see if any rules that are already saved and available.
        List<FileExportRule> existingRules = queryFileExports(args);
        if (null != fsExports) {
            Iterator it = fsExports.keySet().iterator();
            while (it.hasNext()) {
                String fsExpKey = (String) it.next();
                FileExport fileExport = fsObj.getFsExports().get(fsExpKey);
                if ((fileExport.getMountPath() != null) && (fileExport.getMountPath().length() > 0)) {
                    fileExport.setMountPoint(ExportUtils.getFileMountPoint(fileExport.getStoragePort(), fileExport.getMountPath()));
                } else {
                    fileExport.setMountPoint(ExportUtils.getFileMountPoint(fileExport.getStoragePort(), fileExport.getPath()));
                }
                _log.info("FileExport mountpath set to {} {}", fsExpKey, fileExport.getMountPoint());
                // Per New Model of Export Rules Lets create the rule and save it as FileExportRule.
                if (result.isCommandSuccess()) {
                    FileExportRule newRule = getFileExportRule(fsObj.getId(), fileExport, args);
                    _log.debug("ExportRule Constucted per expotkey {}, {}", fsExpKey, newRule);
                    if (existingRules != null && existingRules.isEmpty()) {
                        newRule.setId(URIUtil.createId(FileExportRule.class));
                        _log.info("No Existing rules available for this FS Export and so creating the rule now {}", newRule);
                        _dbClient.createObject(newRule);
                    } else {
                        _log.debug("Checking for existing rule(s) available for this export...");
                        boolean isRuleFound = false;
                        for (FileExportRule rule : existingRules) {
                            _log.debug("Available Export Rule {} - Matching with New Rule {}", rule, newRule);
                            if (newRule.getFsExportIndex() != null && rule.getFsExportIndex().equals(newRule.getFsExportIndex())) {
                                isRuleFound = true;
                                _log.info("Match Found : Skipping this rule as already available {}", newRule);
                                break;
                            }
                        }
                        if (!isRuleFound) {
                            _log.info("Creating new Export Rule {}", newRule);
                            newRule.setId(URIUtil.createId(FileExportRule.class));
                            _dbClient.createObject(newRule);
                            isRuleFound = false;
                        }
                    }
                }
            }
        }
        String eventMsg = result.isCommandSuccess() ? "" : result.getMessage();
        OperationTypeEnum auditType = null;
        auditType = (isFile) ? OperationTypeEnum.EXPORT_FILE_SYSTEM : OperationTypeEnum.EXPORT_FILE_SNAPSHOT;
        fsObj.getOpStatus().updateTaskStatus(opId, result.toOperation());
        if (isFile) {
            recordFileDeviceOperation(_dbClient, auditType, result.isCommandSuccess(), eventMsg, getExportClientExtensions(fileExports), fs, storageObj);
        } else {
            recordFileDeviceOperation(_dbClient, auditType, result.isCommandSuccess(), eventMsg, getExportClientExtensions(fileExports), snapshotObj, fs, storageObj);
        }
        _dbClient.updateObject(fsObj);
        if (result.isCommandSuccess()) {
            WorkflowStepCompleter.stepSucceded(opId);
        }
    } catch (Exception e) {
        ServiceError serviceError = DeviceControllerException.errors.jobFailed(e);
        WorkflowStepCompleter.stepFailed(opId, serviceError);
        String[] params = { storage.toString(), uri.toString(), e.getMessage() };
        _log.error("Unable to export file system or snapshot: storage {}, FS/snapshot URI {}: {}", params);
        for (FileShareExport fsExport : exports) {
            _log.error("{}", fsExport);
        }
        updateTaskStatus(opId, fsObj, e);
        if (URIUtil.isType(uri, FileShare.class)) {
            if ((fs != null) && (storageObj != null)) {
                recordFileDeviceOperation(_dbClient, OperationTypeEnum.EXPORT_FILE_SYSTEM, false, e.getMessage(), "", fs, storageObj);
            }
        } else {
            if ((fs != null) && (storageObj != null) && (snapshotObj != null)) {
                recordFileDeviceOperation(_dbClient, OperationTypeEnum.EXPORT_FILE_SNAPSHOT, false, e.getMessage(), "", snapshotObj, fs, storageObj);
            }
        }
    }
}
Also used : ServiceError(com.emc.storageos.svcs.errorhandling.model.ServiceError) StoragePool(com.emc.storageos.db.client.model.StoragePool) OperationTypeEnum(com.emc.storageos.services.OperationTypeEnum) ArrayList(java.util.ArrayList) FileDeviceInputOutput(com.emc.storageos.volumecontroller.FileDeviceInputOutput) FSExportMap(com.emc.storageos.db.client.model.FSExportMap) FileShare(com.emc.storageos.db.client.model.FileShare) SMBFileShare(com.emc.storageos.db.client.model.SMBFileShare) 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) FileShareExport(com.emc.storageos.volumecontroller.FileShareExport) Snapshot(com.emc.storageos.db.client.model.Snapshot) FileExportRule(com.emc.storageos.db.client.model.FileExportRule) FileExport(com.emc.storageos.db.client.model.FileExport) Iterator(java.util.Iterator) FileObject(com.emc.storageos.db.client.model.FileObject) StorageSystem(com.emc.storageos.db.client.model.StorageSystem)

Example 37 with FileExportRule

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

the class FileDeviceController method doCRUDExports.

private void doCRUDExports(FileExportUpdateParams param, FileShare fs, FileDeviceInputOutput args) throws Exception {
    try {
        // create new exports
        ExportRules exportRules = param.getExportRulesToAdd();
        List<ExportRule> rules;
        if (exportRules != null) {
            rules = exportRules.getExportRules();
            if (rules != null && !rules.isEmpty()) {
                for (ExportRule exportRule : rules) {
                    FileExportRule rule = new FileExportRule();
                    rule.setId(URIUtil.createId(FileExportRule.class));
                    copyPropertiesToSave(rule, exportRule, fs, args);
                    _log.info("Storing New DB Export Rule {}", rule);
                    _dbClient.createObject(rule);
                }
            }
        }
        // Modify Existing Exports
        exportRules = param.getExportRulesToModify();
        if (exportRules != null) {
            rules = exportRules.getExportRules();
            if (rules != null && !rules.isEmpty()) {
                for (ExportRule exportRule : rules) {
                    FileExportRule rule = new FileExportRule();
                    // Copy the properties to build the index id to query DB for existing Export Rule
                    copyPropertiesToSave(rule, exportRule, fs, args);
                    rule = getAvailableExportRule(rule, args);
                    // Remove the existing and create the new one.
                    // Don't Update the existing one as persist object will create a new StringSet rather
                    // it updates the existing one with new information and upon keeping/appending to old one.
                    rule.setInactive(true);
                    _log.info("Removing Existing DB Export Rule {}", rule);
                    _dbClient.updateObject(rule);
                    FileExportRule newRule = new FileExportRule();
                    newRule.setId(URIUtil.createId(FileExportRule.class));
                    // Now, Copy the properties again into the rule came out of DB, before updating.
                    copyPropertiesToSave(newRule, exportRule, fs, args);
                    _log.info("Storing New DB Export Rule {}", newRule);
                    _dbClient.createObject(newRule);
                }
            }
        }
        // Delete Existing Exports
        exportRules = param.getExportRulesToDelete();
        if (exportRules != null) {
            rules = exportRules.getExportRules();
            if (rules != null && !rules.isEmpty()) {
                for (ExportRule exportRule : rules) {
                    FileExportRule rule = new FileExportRule();
                    copyPropertiesToSave(rule, exportRule, fs, args);
                    rule = getAvailableExportRule(rule, args);
                    rule.setInactive(true);
                    _log.info("Marking DB Export Rule Inactive {}", rule);
                    _dbClient.updateObject(rule);
                }
            }
            // Delete the ExportMap entry if there are no export rules for this file system or sub directory
            FSExportMap fsNFSExportMap = fs.getFsExports();
            ContainmentConstraint containmentConstraint = ContainmentConstraint.Factory.getFileExportRulesConstraint(fs.getId());
            List<FileExportRule> fileExportRules = CustomQueryUtility.queryActiveResourcesByConstraint(_dbClient, FileExportRule.class, containmentConstraint);
            Set<String> fileExportMapKeys = fsNFSExportMap.keySet();
            Iterator<String> keySetIterator = fileExportMapKeys.iterator();
            HashSet<String> keystoRemove = new HashSet<String>();
            while (keySetIterator.hasNext()) {
                String fileExportMapKey = keySetIterator.next();
                FileExport fileExport = fsNFSExportMap.get(fileExportMapKey);
                boolean exportRuleExists = false;
                for (FileExportRule fileExportRule : fileExportRules) {
                    if (fileExportRule.getExportPath().equals(fileExport.getMountPath())) {
                        exportRuleExists = true;
                        break;
                    }
                }
                if (!exportRuleExists) {
                    keystoRemove.add(fileExportMapKey);
                }
            }
            for (String key : keystoRemove) {
                _log.info("Deleting file export map entry : {} for key : {}", fsNFSExportMap.get(key), key);
                fsNFSExportMap.remove(key);
            }
            _dbClient.updateObject(fs);
        }
    } catch (Exception e) {
        _log.info("Error While executing CRUD Operations {}", e);
    }
}
Also used : ContainmentConstraint(com.emc.storageos.db.client.constraint.ContainmentConstraint) FSExportMap(com.emc.storageos.db.client.model.FSExportMap) 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) ExportRules(com.emc.storageos.model.file.ExportRules) FileExportRule(com.emc.storageos.db.client.model.FileExportRule) FileExport(com.emc.storageos.db.client.model.FileExport) FileExportRule(com.emc.storageos.db.client.model.FileExportRule) ExportRule(com.emc.storageos.model.file.ExportRule) HashSet(java.util.HashSet)

Aggregations

FileExportRule (com.emc.storageos.db.client.model.FileExportRule)37 URI (java.net.URI)13 APIException (com.emc.storageos.svcs.errorhandling.resources.APIException)11 URISyntaxException (java.net.URISyntaxException)11 ContainmentConstraint (com.emc.storageos.db.client.constraint.ContainmentConstraint)10 FileShare (com.emc.storageos.db.client.model.FileShare)10 DatabaseException (com.emc.storageos.db.exceptions.DatabaseException)10 ExportRule (com.emc.storageos.model.file.ExportRule)10 InternalException (com.emc.storageos.svcs.errorhandling.resources.InternalException)10 ArrayList (java.util.ArrayList)8 ControllerException (com.emc.storageos.volumecontroller.ControllerException)7 URIQueryResultList (com.emc.storageos.db.client.constraint.URIQueryResultList)6 SMBFileShare (com.emc.storageos.db.client.model.SMBFileShare)6 Snapshot (com.emc.storageos.db.client.model.Snapshot)6 StorageSystem (com.emc.storageos.db.client.model.StorageSystem)6 FSExportMap (com.emc.storageos.db.client.model.FSExportMap)5 FileExport (com.emc.storageos.db.client.model.FileExport)5 DeviceControllerException (com.emc.storageos.exceptions.DeviceControllerException)5 WorkflowException (com.emc.storageos.workflow.WorkflowException)5 StringSet (com.emc.storageos.db.client.model.StringSet)4