Search in sources :

Example 1 with Input

use of com.emc.storageos.model.customservices.CustomServicesWorkflowDocument.Input in project coprhd-controller by CoprHD.

the class ValidationHelper method validateInput.

private Map<String, CustomServicesValidationResponse.ErrorInput> validateInput(final String stepId, final List<Input> stepInputList, final boolean cycleExists) {
    final Map<String, CustomServicesValidationResponse.ErrorInput> errorInputMap = new HashMap<>();
    final Set<String> uniqueInputNames = new HashSet<>();
    for (final Input input : stepInputList) {
        final CustomServicesValidationResponse.ErrorInput errorInput = new CustomServicesValidationResponse.ErrorInput();
        final List<String> errorMessages = new ArrayList<>();
        final String inputTypeErrorMessage = checkInputType(stepId, input, cycleExists);
        if (!inputTypeErrorMessage.isEmpty()) {
            errorMessages.add(inputTypeErrorMessage);
        }
        // Enforce friendly name uniqueness only for those input that will be displayed in the order page and need user input selection.
        if (StringUtils.isNotBlank(input.getType()) && !(input.getType().equals(CustomServicesConstants.InputType.FROM_STEP_INPUT.toString()) || input.getType().equals(CustomServicesConstants.InputType.FROM_STEP_OUTPUT.toString()) || input.getType().equals(CustomServicesConstants.InputType.DISABLED.toString()))) {
            final String uniqueFriendlyNameErrorMessage = checkUniqueNames(true, input.getFriendlyName(), uniqueFriendlyInputNames);
            if (!uniqueFriendlyNameErrorMessage.isEmpty()) {
                errorMessages.add(uniqueFriendlyNameErrorMessage);
            }
        }
        // Enforce uniqueness for all input names in the input group
        // TODO: This might be revisited based on the discussion of unique names in step vs step input group
        final String uniqueInputNameErrorMessage = checkUniqueNames(false, input.getName(), uniqueInputNames);
        if (!uniqueInputNameErrorMessage.isEmpty()) {
            errorMessages.add(uniqueInputNameErrorMessage);
        }
        if (!errorMessages.isEmpty()) {
            errorInput.setErrorMessages(errorMessages);
            errorInputMap.put(input.getName(), errorInput);
        }
    }
    return errorInputMap;
}
Also used : Input(com.emc.storageos.model.customservices.CustomServicesWorkflowDocument.Input) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CustomServicesValidationResponse(com.emc.storageos.model.customservices.CustomServicesValidationResponse) HashSet(java.util.HashSet)

Example 2 with Input

use of com.emc.storageos.model.customservices.CustomServicesWorkflowDocument.Input in project coprhd-controller by CoprHD.

the class WorkflowServiceDescriptor method mapWorkflowToServiceDescriptor.

private ServiceDescriptor mapWorkflowToServiceDescriptor(final CustomServicesWorkflow from) {
    final ServiceDescriptor to = new ServiceDescriptor();
    try {
        final CustomServicesWorkflowDocument wfDocument = WorkflowHelper.toWorkflowDocument(from);
        if (StringUtils.isEmpty(from.getState()) || from.getState().equals(CustomServicesWorkflowStatus.NONE) || from.getState().equals(CustomServicesWorkflowStatus.INVALID)) {
            log.error("Workflow state is not valid. State:{} Workflow name:{}", from.getState(), from.getLabel());
            throw new IllegalStateException(String.format("Workflow state is not valid. State %s Workflow name: %s Workflow id: %s", from.getState(), from.getLabel(), from.getId()));
        }
        to.setCategory(CUSTOM_SERVICE_CATEGORY);
        to.setDescription(StringUtils.isNotBlank(wfDocument.getDescription()) ? wfDocument.getDescription() : wfDocument.getName());
        to.setDestructive(false);
        final String wfID = URIUtil.asString(from.getId());
        to.setServiceId(StringUtils.isNotBlank(wfID) ? wfID : wfDocument.getName());
        to.setTitle(wfDocument.getName());
        to.setWorkflowId(wfDocument.getName());
        to.setRoles(new ArrayList<String>(Arrays.asList(Role.SYSTEM_ADMIN.toString())));
        for (final Step step : wfDocument.getSteps()) {
            if (null != step.getInputGroups()) {
                // Looping through all input groups
                for (final InputGroup inputGroup : step.getInputGroups().values()) {
                    final MultiValueMap tableMap = new MultiValueMap();
                    for (final Input wfInput : inputGroup.getInputGroup()) {
                        final ServiceField serviceField = new ServiceField();
                        if (CustomServicesConstants.InputType.FROM_USER.toString().equals(wfInput.getType())) {
                            serviceField.setType(wfInput.getInputFieldType());
                        } else if (CustomServicesConstants.InputType.ASSET_OPTION_SINGLE.toString().equals(wfInput.getType())) {
                            serviceField.setType(wfInput.getValue());
                        } else if (CustomServicesConstants.InputType.ASSET_OPTION_MULTI.toString().equals(wfInput.getType())) {
                            serviceField.setType(wfInput.getValue());
                            serviceField.setSelect(ServiceField.SELECT_MANY);
                        } else if (CustomServicesConstants.InputType.FROM_USER_MULTI.toString().equals(wfInput.getType())) {
                            serviceField.setType(ServiceField.TYPE_CHOICE);
                            if (StringUtils.isNotBlank(wfInput.getDefaultValue())) {
                                // For list of options
                                final Map<String, String> options = new HashMap<>();
                                final List<String> defaultList = Arrays.asList(wfInput.getDefaultValue().split(","));
                                for (final String value : defaultList) {
                                    // making the key and value the same
                                    options.put(value, value);
                                }
                                serviceField.setOptions(options);
                                serviceField.setInitialValue(options.get(defaultList.get(0)));
                            } else if (MapUtils.isNotEmpty(wfInput.getOptions())) {
                                // For options Map
                                serviceField.setOptions(wfInput.getOptions());
                            }
                        } else {
                            continue;
                        }
                        final String inputName = wfInput.getName();
                        if (StringUtils.isNotBlank(wfInput.getDescription())) {
                            serviceField.setDescription(wfInput.getDescription());
                        }
                        final String friendlyName = StringUtils.isBlank(wfInput.getFriendlyName()) ? inputName : wfInput.getFriendlyName();
                        serviceField.setLabel(friendlyName);
                        serviceField.setName(friendlyName.replaceAll(CustomServicesConstants.SPACES_REGEX, StringUtils.EMPTY));
                        serviceField.setRequired(wfInput.getRequired());
                        if (!(CustomServicesConstants.InputType.FROM_USER_MULTI.toString().equals(wfInput.getType()))) {
                            // Initial value already set for FROM_USER_MULTI
                            serviceField.setInitialValue(wfInput.getDefaultValue());
                        }
                        // Setting all unlocked fields as lockable
                        if (!wfInput.getLocked()) {
                            serviceField.setLockable(true);
                        }
                        // if there is a table name we will build ServiceFieldTable later
                        if (null != wfInput.getTableName()) {
                            tableMap.put(wfInput.getTableName(), serviceField);
                        } else {
                            to.getItems().put(friendlyName, serviceField);
                        }
                    }
                    for (final String table : (Set<String>) tableMap.keySet()) {
                        final ServiceFieldTable serviceFieldTable = new ServiceFieldTable();
                        serviceFieldTable.setType(ServiceItem.TYPE_TABLE);
                        serviceFieldTable.setLabel(table);
                        serviceFieldTable.setName(table);
                        for (final ServiceField serviceField : (List<ServiceField>) tableMap.getCollection(table)) {
                            serviceFieldTable.addItem(serviceField);
                        }
                        to.getItems().put(table, serviceFieldTable);
                    }
                }
            }
        }
    } catch (final IOException io) {
        log.error("Error deserializing workflow", io);
        throw new IllegalStateException(String.format("Error deserializing workflow %s", from.getLabel()));
    }
    log.debug("Mapped workflow service descriptor for {}", from.getLabel());
    return to;
}
Also used : Set(java.util.Set) ServiceFieldTable(com.emc.sa.descriptor.ServiceFieldTable) CustomServicesWorkflowDocument(com.emc.storageos.model.customservices.CustomServicesWorkflowDocument) Step(com.emc.storageos.model.customservices.CustomServicesWorkflowDocument.Step) IOException(java.io.IOException) InputGroup(com.emc.storageos.model.customservices.CustomServicesWorkflowDocument.InputGroup) Input(com.emc.storageos.model.customservices.CustomServicesWorkflowDocument.Input) ServiceDescriptor(com.emc.sa.descriptor.ServiceDescriptor) ServiceField(com.emc.sa.descriptor.ServiceField) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) MultiValueMap(org.apache.commons.collections.map.MultiValueMap) Map(java.util.Map) MultiValueMap(org.apache.commons.collections.map.MultiValueMap)

Example 3 with Input

use of com.emc.storageos.model.customservices.CustomServicesWorkflowDocument.Input in project coprhd-controller by CoprHD.

the class CustomServicesService method updateInputPerStep.

/**
 * Method to collect all required inputs per step for execution
 * Case :
 * SingleUserInput , InputFromUserMulti and AssetOptionSingle
 * The order form sends the value as String with double quotes for each value, we remove the quotes
 * the reason for splitting by ',' is, for table type cases, where the input name is part of a table and hence
 * we should store it as a list of values
 * Eg., of single input inside a table: say the name is "volume"
 * from order context value for name (which is part of table will be) = ""vol1","vol2","vol3""
 * These will be stored in the inputs Map as follows:
 * input[key] = "volumes"
 * input[value][0]=vol1
 * input[value][1]=vol2
 * input[value][2]=vol3
 *
 * Case 2: AssetOptionMulti
 * The array can be passed by itself or as part of a table
 * Since the order form sends the value as String with double quotes for each value, we remove the quotes
 * the reason for splitting by "," is for table type cases, where the input name is part of a table and hence we
 * should store it as a list of values
 * Eg., of array input without table:
 * arrayInputWithouttable = ""vol1","vol2","vol3""
 * These will be stored in the inputs Map as follows:
 * input[key] = "volumes"
 * input[value][0]=vol1,vol2,vol3
 *
 * Eg., of array input with table:
 * arrayInputWithtable = ""1,2,3","4","14,15","24,25,26"" ie., in a table (complex structure), the array input
 * is clubbed by row and separated by commans
 *
 * These will be stored in the inputs Map as follows:
 * input[key] = "volumes"
 * input[value][0]=1,2,3
 * input[value][1]=4
 * input[value][2]=14,15
 * input[value][3]=24,25,26
 *
 * @param step It is the JSON Object of Step
 */
private void updateInputPerStep(final Step step) throws Exception {
    if (!updateInput(step)) {
        return;
    }
    final Map<String, List<String>> inputs = new HashMap<String, List<String>>();
    for (final CustomServicesWorkflowDocument.InputGroup inputGroup : step.getInputGroups().values()) {
        for (final Input value : inputGroup.getInputGroup()) {
            final String name = value.getName();
            String friendlyName = value.getFriendlyName();
            if (friendlyName != null) {
                friendlyName = friendlyName.replaceAll(CustomServicesConstants.SPACES_REGEX, StringUtils.EMPTY);
            }
            if (StringUtils.isEmpty(value.getType())) {
                continue;
            }
            switch(InputType.fromString(value.getType())) {
                case DISABLED:
                    inputs.put(name, Arrays.asList(""));
                    break;
                case FROM_USER:
                case FROM_USER_MULTI:
                case ASSET_OPTION_SINGLE:
                    if (params.get(friendlyName) != null && !StringUtils.isEmpty(params.get(friendlyName).toString())) {
                        final String param;
                        if (!StringUtils.isEmpty(value.getInputFieldType()) && value.getInputFieldType().toUpperCase().equals(CustomServicesConstants.InputFieldType.PASSWORD.toString())) {
                            param = decrypt(params.get(friendlyName).toString());
                        } else {
                            param = params.get(friendlyName).toString();
                        }
                        if (StringUtils.isEmpty(value.getTableName())) {
                            inputs.put(name, Arrays.asList(param.replace("\"", "")));
                        } else {
                            inputs.put(name, Arrays.asList(param.replace("\"", "").split(",")));
                        }
                    } else {
                        if (value.getDefaultValue() != null) {
                            inputs.put(name, Arrays.asList(value.getDefaultValue()));
                        } else {
                            inputs.put(name, Arrays.asList(""));
                        }
                    }
                    break;
                case ASSET_OPTION_MULTI:
                    if (params.get(friendlyName) != null && !StringUtils.isEmpty(params.get(friendlyName).toString())) {
                        final List<String> arrayInput;
                        if (!StringUtils.isEmpty(value.getTableName())) {
                            arrayInput = Arrays.asList(params.get(friendlyName).toString().split("\",\""));
                        } else {
                            arrayInput = Arrays.asList(params.get(friendlyName).toString());
                        }
                        int index = 0;
                        for (String eachVal : arrayInput) {
                            arrayInput.set(index++, eachVal.replace("\"", ""));
                        }
                        inputs.put(name, arrayInput);
                    } else {
                        if (value.getDefaultValue() != null) {
                            // The default value is copied only for the first index
                            // in case of table type, it is not evident how many times the default value need to be copied.
                            inputs.put(name, Arrays.asList(value.getDefaultValue()));
                        } else {
                            inputs.put(name, Arrays.asList(""));
                        }
                    }
                    break;
                case FROM_STEP_INPUT:
                case FROM_STEP_OUTPUT:
                    {
                        final String[] paramVal = value.getValue().split("\\.", 2);
                        final String stepId = paramVal[CustomServicesConstants.STEP_ID];
                        final String attribute = paramVal[CustomServicesConstants.INPUT_FIELD];
                        Map<String, List<String>> stepInput;
                        boolean fromStepOutput = true;
                        if (value.getType().equals(InputType.FROM_STEP_INPUT.toString())) {
                            stepInput = inputPerStep.get(stepId);
                            fromStepOutput = false;
                        } else {
                            stepInput = outputPerStep.get(stepId);
                        }
                        if (stepInput != null && stepInput.get(attribute) != null) {
                            if (fromStepOutput && StringUtils.isEmpty(value.getTableName())) {
                                inputs.put(name, Arrays.asList(String.join(", ", stepInput.get(attribute)).replace("\"", "")));
                                break;
                            } else {
                                inputs.put(name, stepInput.get(attribute));
                                break;
                            }
                        } else {
                            if (value.getRequired()) {
                                throw InternalServerErrorException.internalServerErrors.customServiceExecutionFailed("Value mapped is null : " + value.getValue());
                            }
                        }
                        if (value.getDefaultValue() != null) {
                            inputs.put(name, Arrays.asList(value.getDefaultValue()));
                        } else {
                            inputs.put(name, Arrays.asList(""));
                        }
                        break;
                    }
                default:
                    throw InternalServerErrorException.internalServerErrors.customServiceExecutionFailed("Invalid input type:" + value.getType());
            }
        }
    }
    inputPerStep.put(step.getId(), inputs);
}
Also used : Input(com.emc.storageos.model.customservices.CustomServicesWorkflowDocument.Input) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) CustomServicesWorkflowDocument(com.emc.storageos.model.customservices.CustomServicesWorkflowDocument) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

Input (com.emc.storageos.model.customservices.CustomServicesWorkflowDocument.Input)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 CustomServicesWorkflowDocument (com.emc.storageos.model.customservices.CustomServicesWorkflowDocument)2 List (java.util.List)2 Map (java.util.Map)2 ServiceDescriptor (com.emc.sa.descriptor.ServiceDescriptor)1 ServiceField (com.emc.sa.descriptor.ServiceField)1 ServiceFieldTable (com.emc.sa.descriptor.ServiceFieldTable)1 CustomServicesValidationResponse (com.emc.storageos.model.customservices.CustomServicesValidationResponse)1 InputGroup (com.emc.storageos.model.customservices.CustomServicesWorkflowDocument.InputGroup)1 Step (com.emc.storageos.model.customservices.CustomServicesWorkflowDocument.Step)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 MultiValueMap (org.apache.commons.collections.map.MultiValueMap)1