+ private Collection<SubPropertyToscaFunction> buildSubPropertyToscaFunctions(final Map<String, Object> propValueMap, final List<String> path) {
+ Collection<SubPropertyToscaFunction> subPropertyToscaFunctions = new ArrayList<>();
+ propValueMap.entrySet().stream().filter(entry -> entry.getValue() instanceof Map).forEach(entry -> {
+ List<String> subPropertyPath = new ArrayList<>(path);
+ subPropertyPath.add(entry.getKey());
+ if (ToscaFunctionType.findType(((Map<String, Object>) entry.getValue()).keySet().iterator().next()).isPresent()) {
+ Optional<ToscaFunction> toscaFunction =
+ toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue((Map) entry.getValue());
+ if (toscaFunction.isPresent()) {
+ SubPropertyToscaFunction subPropertyToscaFunction = new SubPropertyToscaFunction();
+ subPropertyToscaFunction.setToscaFunction(toscaFunction.get());
+ subPropertyToscaFunction.setSubPropertyPath(subPropertyPath);
+ subPropertyToscaFunctions.add(subPropertyToscaFunction);
+ }
+ } else {
+ subPropertyToscaFunctions.addAll(buildSubPropertyToscaFunctions((Map<String, Object>) entry.getValue(), subPropertyPath));
+ }
+ });
+ return subPropertyToscaFunctions;
+ }
+
+ private UploadInterfaceInfo buildInterface(String interfaceName, Object interfaceValue) {
+ UploadInterfaceInfo interfaceDef = new UploadInterfaceInfo();
+ interfaceDef.setValue(interfaceValue);
+ interfaceDef.setName(interfaceName);
+ interfaceDef.setKey(interfaceName);
+ Map<String, OperationDataDefinition> operations = new HashMap<>();
+ if (interfaceValue instanceof Map) {
+ Map<String, Object> operationsMap = (Map<String, Object>) interfaceValue;
+ for (Map.Entry<String, Object> operationEntry : operationsMap.entrySet()) {
+ OperationDataDefinition operationDef = new OperationDataDefinition();
+ operationDef.setName(operationEntry.getKey());
+ Map<String, Object> operationValue = (Map<String, Object>) operationEntry.getValue();
+ if (operationValue.containsKey(DESCRIPTION.getElementName())) {
+ operationDef.setDescription(operationValue.get(DESCRIPTION.getElementName()).toString());
+ }
+ operationDef.setImplementation(handleOperationImplementation(operationValue).orElse(new ArtifactDataDefinition()));
+ if (operationValue.containsKey(INPUTS.getElementName())) {
+ final Map<String, Object> interfaceInputs = (Map<String, Object>) operationValue.get(INPUTS.getElementName());
+ operationDef.setInputs(handleInterfaceOperationInputs(interfaceInputs));
+ }
+ operations.put(operationEntry.getKey(), operationDef);
+ }
+ interfaceDef.setOperations(operations);
+ if (operationsMap.containsKey(TYPE.getElementName())) {
+ interfaceDef.setType(((Map<String, Object>) interfaceValue).get(TYPE.getElementName()).toString());
+ }
+ }
+ return interfaceDef;
+ }
+
+ private ListDataDefinition<OperationInputDefinition> handleInterfaceOperationInputs(final Map<String, Object> interfaceInputs) {
+ final ListDataDefinition<OperationInputDefinition> inputs = new ListDataDefinition<>();
+ for (final Entry<String, Object> interfaceInput : interfaceInputs.entrySet()) {
+ final OperationInputDefinition operationInput = new OperationInputDefinition();
+ operationInput.setUniqueId(UUID.randomUUID().toString());
+ operationInput.setInputId(operationInput.getUniqueId());
+ operationInput.setName(interfaceInput.getKey());
+
+ handleInputToscaDefinition(interfaceInput.getKey(), interfaceInput.getValue(), operationInput);
+ inputs.add(operationInput);
+ }
+ return inputs;
+ }
+
+ private void handleInputToscaDefinition(
+ final String inputName,
+ final Object value,
+ final OperationInputDefinition operationInput
+ ) {
+ if (value instanceof Map) {
+ final Map<String, Object> valueMap = (Map<String, Object>) value;
+ log.debug("Creating interface operation input '{}'", inputName);
+ Type type = new TypeToken<LinkedHashMap<String, Object>>() {
+ }.getType();
+ String stringValue = gson.toJson(value, type);
+ if (toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(value)) {
+ toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue((Map<String, Object>) value)
+ .ifPresent(operationInput::setToscaFunction);
+ } else {
+ final Collection<SubPropertyToscaFunction> subPropertyToscaFunctions = buildSubPropertyToscaFunctions(valueMap, new ArrayList<>());
+ if (CollectionUtils.isNotEmpty(subPropertyToscaFunctions)) {
+ Collection<SubPropertyToscaFunction> existingSubPropertyToscaFunctions = operationInput.getSubPropertyToscaFunctions();
+ if (existingSubPropertyToscaFunctions == null) {
+ operationInput.setSubPropertyToscaFunctions(subPropertyToscaFunctions);
+ } else {
+ operationInput.getSubPropertyToscaFunctions().addAll(subPropertyToscaFunctions);
+ }
+ }
+ }
+ operationInput.setValue(stringValue);
+ }
+ if (value instanceof String) {
+ final String stringValue = (String) value;
+ operationInput.setDefaultValue(stringValue);
+ operationInput.setToscaDefaultValue(stringValue);
+ operationInput.setValue(stringValue);
+ }
+ operationInput.setType("string");
+ if (operationInput.getValue() == null) {
+ operationInput.setValue(String.valueOf(value));
+ }
+ }
+
+ private Optional<ArtifactDataDefinition> handleOperationImplementation(
+ final Map<String, Object> operationDefinitionMap
+ ) {
+ if (!operationDefinitionMap.containsKey(IMPLEMENTATION.getElementName())) {
+ return Optional.empty();
+ }
+ final ArtifactDataDefinition artifactDataDefinition = new ArtifactDataDefinition();
+ if (operationDefinitionMap.get(IMPLEMENTATION.getElementName()) instanceof Map &&
+ ((Map) operationDefinitionMap.get(IMPLEMENTATION.getElementName())).containsKey("primary")) {
+
+ final Object primary = ((Map) operationDefinitionMap.get(IMPLEMENTATION.getElementName())).get("primary");
+ if (primary instanceof Map) {
+ Map<String, Object> implDetails = (Map) primary;
+
+ if (implDetails.get("file") != null) {
+ final String file = implDetails.get("file").toString();
+ artifactDataDefinition.setArtifactName(generateArtifactName(file));
+ }
+ if (implDetails.get("type") != null) {
+ artifactDataDefinition.setArtifactType(implDetails.get("type").toString());
+ }
+ if (implDetails.get("artifact_version") != null) {
+ artifactDataDefinition.setArtifactVersion(implDetails.get("artifact_version").toString());
+ }
+
+ if (implDetails.get("properties") instanceof Map) {
+ Map<String, Object> properties = (Map<String, Object>) implDetails.get("properties");
+ properties.forEach((k, v) -> {
+ ToscaPropertyType type = getTypeFromObject(v);
+ if (type != null) {
+ PropertyDataDefinition propertyDef = new PropertyDataDefinition();
+ propertyDef.setName(k);
+ propertyDef.setValue(v.toString());
+ artifactDataDefinition.addProperty(propertyDef);
+ }
+ });
+ }
+ } else {
+ artifactDataDefinition.setArtifactName(generateArtifactName(primary.toString()));
+ }
+ }
+
+ if (operationDefinitionMap.get(IMPLEMENTATION.getElementName()) instanceof Map &&
+ ((Map) operationDefinitionMap.get(IMPLEMENTATION.getElementName())).containsKey("timeout")) {
+ final Object timeOut = ((Map) operationDefinitionMap.get(IMPLEMENTATION.getElementName())).get("timeout");
+ artifactDataDefinition.setTimeout((Integer)timeOut);
+ }
+
+ if (operationDefinitionMap.get(IMPLEMENTATION.getElementName()) instanceof String) {
+ final String implementation = (String) operationDefinitionMap.get(IMPLEMENTATION.getElementName());
+ artifactDataDefinition.setArtifactName(generateArtifactName(implementation));
+ }
+ return Optional.of(artifactDataDefinition);
+ }
+
+ private String generateArtifactName(final String name) {
+ if (OperationArtifactUtil.artifactNameIsALiteralValue(name)) {
+ return name;
+ } else {
+ return QUOTE + name + QUOTE;
+ }
+ }
+
+ private ToscaPropertyType getTypeFromObject(final Object value) {
+ if (value instanceof String) {
+ return ToscaPropertyType.STRING;
+ }
+ if (value instanceof Integer) {
+ return ToscaPropertyType.INTEGER;
+ }
+ if (value instanceof Boolean) {
+ return ToscaPropertyType.BOOLEAN;
+ }
+ if (value instanceof Float || value instanceof Double) {
+ return ToscaPropertyType.FLOAT;
+ }
+ return null;
+ }
+